Comments in Kotlin
📌 Comments in Kotlin
In Kotlin, comments are used to add explanations or notes within the code. They are ignored by the compiler and have no effect on program execution.
Kotlin supports two types of comments:
Single-line Comments
Multi-line Comments
✅ 1. Single-line Comments
Single-line comments start with
//.Used for short explanations or notes.
📌 Example:
val name = "John"// Declare a variableprintln(name) // Print the name
✅ 2. Multi-line Comments
Multi-line comments start with
/*and end with*/.Useful for adding detailed explanations or temporarily disabling blocks of code.
📌 Example:
/* This is a multi-line comment. It can span multiple lines and is useful for detailed documentation.*/val age = 25println("Age: $age")
✅ 3. Nested Comments
Kotlin supports nested comments inside multi-line comments.
This is useful for debugging large code blocks.
📌 Example:
/* Outer comment /* Nested comment */ println("Hello, World!") // This line will still execute*/
✅ 4. Documentation Comments
Kotlin supports KDoc (Kotlin Documentation) using
/** ... */.It is used for generating documentation for classes, functions, or properties.
📌 Example:
/** * This function adds two numbers. * * @param a First number * @param b Second number * @return Sum of a and b */fun(a: Int, b: ): Int { return a + b}println(add(5, 10)) // Output: 15
@param→ Describes function parameters@return→ Describes the return value
✅ Conclusion
Use
//for short comments.Use
/* */for longer comments or debugging.Use
/** */for documenting functions, classes, and properties using KDoc.