Menu
Coddy logo textTech

Comments

Part of the Fundamentals section of Coddy's Kotlin journey. Lesson 3 of 93.

Comments are used to add explanatory notes within the code. They are ignored by the compiler and are meant for human understanding.

In Kotlin, there are two ways to write comments:

Single-line comments using //:

fun main() {
    // This is a comment
    println("Hello!")  // This prints "Hello!"

    // You can use comments to explain your code
    println("Kotlin is fun!")  // This will show on the screen
}

Multi-line comments using /* */:

fun main() {
    /*
    This is a multi-line comment.
    You can write multiple lines
    of explanation here.
    */
    println("Welcome!")
}

Common Comment Practices:

1. TODO Comments - Mark future tasks:

fun main() {
    // TODO: Add error handling for invalid inputs
    println("Processing...")
}

2. Disabling code temporarily:

fun main() {
    // println("This line is disabled")
    println("This line will execute")
}

3. Explaining complex logic:

fun main() {
    // Calculate the average of the list
    val total = 100
    val count = 5
    val average = total / count
    println(average)
}
challenge icon

Challenge

Beginner

Fix the code so that only Hello, Kotlin! is printed.

  • Replace the ? with the symbols that turn a line into a comment
  • The line printing Goodbye! should become a comment so it does NOT run
  • Only change the line that starts with ?

Try it yourself

fun main() {
    // Type your code below
    ? println("Goodbye!")
    println("Hello, Kotlin!")
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals

Practice on your own: Kotlin playground