Member-only story

Annotations in Kotlin: The Secret Sauce for Powerful Code 🚀

Jayant Kumar🇮🇳
3 min readMar 7, 2025

--

Photo by Sander Sammy on Unsplash

Link For Non-Members

Have you ever seen weird-looking symbols like @Serializable, @Deprecated, or @Inject in Kotlin code and wondered what they do? 🤔

These are annotations, a powerful feature in Kotlin that lets you add metadata to your code. In simple words, annotations provide extra information to the compiler, tools, or frameworks without affecting how the code runs.

In this article, we’ll explore what annotations are, and when to use them with real-world examples. Let’s dive in! 🚀

What Are Annotations in Kotlin?

Annotations in Kotlin are special markers (metadata) that help tools or the Kotlin compiler process your code in a better way.

Example of an Annotation in Action

@Deprecated("Use newFunction() instead of oldFunction()", ReplaceWith("newFunction()"))
fun oldFunction() {
println("This is an old function")
}

fun newFunction() {
println("This is the new function")
}

fun main() {
oldFunction() // Warning: This function is deprecated
}

✅ Here, @Deprecated tells the compiler that oldFunction() is outdated and suggests using newFunction() instead.

Built-in Annotations in Kotlin

Kotlin provides several useful annotations. Here are some common ones:

@Deprecated — Marks Old Code

If you have old functions or classes that should not be used, you can mark them as deprecated.

@Deprecated("Use calculateNewTax() instead")
fun calculateOldTax() {
println("Old tax calculation")
}

✅ This helps warn developers that they should switch to a newer method.

--

--

Jayant Kumar🇮🇳
Jayant Kumar🇮🇳

Written by Jayant Kumar🇮🇳

Hello My name is Jayant Kumar, I am a software Engineer , specialist in Mobile Development (Android , IOS , Flutter , React Native) from India 🇮🇳

No responses yet

Write a response