Kotlin Comments

A comment is text in your code that the compiler ignores completely. Comments exist only for humans — to explain what the code does, why a decision was made, or to temporarily disable a piece of code during testing.

Types of Comments in Kotlin

Kotlin supports three types of comments:

1. Single-Line Comment

Use // to comment out one line. Everything after // on that line is ignored.

// This is a single-line comment
println("Hello")  // This prints a greeting to the screen

// val unusedVariable = 10  ← this line is ignored by the compiler

2. Multi-Line Comment

Use /* to start and */ to end. Everything between them is ignored, even across many lines.

/*
  This function calculates the area of a rectangle.
  It takes width and height as input.
  It returns the result as an Int.
*/
fun area(width: Int, height: Int): Int {
    return width * height
}

3. KDoc Comment

KDoc is a special comment format for documenting public functions and classes. It starts with /** and ends with */. Tools like IntelliJ IDEA use KDoc to display help text when you hover over a function.

/**
 * Calculates the area of a rectangle.
 *
 * @param width The width of the rectangle in centimeters.
 * @param height The height of the rectangle in centimeters.
 * @return The area as an integer value.
 */
fun area(width: Int, height: Int): Int {
    return width * height
}

How Comments Work: Visual Diagram


Source Code (.kt file)
─────────────────────────────────────────────
// Step 1: Add the two numbers              ← Comment (ignored)
val sum = 5 + 3                             ← Code (executed)
println(sum)                                ← Code (executed)
/* This was an old approach:
   val sum = calculate(5, 3)               ← Comment (ignored)
*/
─────────────────────────────────────────────
         │
         ▼
  Compiler sees only:
  val sum = 5 + 3
  println(sum)
─────────────────────────────────────────────

Nested Multi-Line Comments

Kotlin supports nested multi-line comments. This is useful when commenting out a block that already contains another comment inside it.

/*
    val x = 10
    /* inner comment */
    val y = 20
*/

Java does not support nested comments, but Kotlin does. This is a small but helpful difference.

When to Use Comments

Good use: Explaining why, not what

// Using Int instead of Long here because values never exceed 1000
val count: Int = 0

Bad use: Restating what the code already says clearly

// Print hello
println("Hello")  ← the comment adds no extra information

Good use: Temporarily disabling code

fun main() {
    val result = add(3, 4)
    // val result = subtract(3, 4)  ← disabled for now
    println(result)
}

Comments in Practice

fun main() {
    // Define the price of a product
    val price = 250

    // Apply a 10% discount
    val discount = price * 0.10

    /*
      Calculate the final amount after discount.
      We use Double because discount is a decimal.
    */
    val finalPrice = price - discount

    println("Final price: $finalPrice")
}

Each comment here explains a decision or a step, making the code easy to understand for anyone reading it later — including yourself six months from now.

Leave a Comment

Your email address will not be published. Required fields are marked *