Comments help explain your code so others (and you) can understand it later. Documentation comments create helpful guides for your code.
Comments and documentation syntax in Kotlin
// This is a single-line comment /* This is a multi-line comment */ /** * This is a documentation comment. * It is used to describe classes, functions, or properties. */
Single-line comments start with // and go to the end of the line.
Multi-line comments start with /* and end with */. They can span many lines.
// This is a single-line comment val x = 5
/* This is a multi-line comment */ val y = 10
add function. It helps generate documentation automatically./** * Adds two numbers and returns the result. * @param a first number * @param b second number * @return sum of a and b */ fun add(a: Int, b: Int): Int { return a + b }
This program uses comments to explain what the code does. The documentation comment describes the add function, and single-line comments explain lines inside the code.
// This program adds two numbers /** * Adds two integers and returns the sum. * * @param a first integer * @param b second integer * @return sum of a and b */ fun add(a: Int, b: Int): Int { // Return the sum return a + b } fun main() { val result = add(3, 4) // Call add function println("The sum is: $result") }
Use comments to make your code easier to understand, but avoid obvious comments that just repeat the code.
Documentation comments start with /** and are used by tools to create API docs.
Keep comments up to date when you change your code to avoid confusion.
Comments explain your code and help others understand it.
Use // for single-line and /* ... */ for multi-line comments.
Documentation comments /** ... */ describe code elements and help generate docs.