CodeBlanks Logo Icon CodeBlanks

Comments

Learn to write // and /* */ comments in Kotlin.

Comments: // and /* */

Comments are notes the Kotlin compiler ignores. Single-line comments start with // and run to the end of the line. Block comments start with /* and end with */; they can span multiple lines.

Comments explain code; they do not affect the compiled output. Clear names and short comments keep code readable.

// a single-line comment
/* this is
   a block
   comment */

KDoc /** */

Kotlin offers a special comment form called KDoc for documentation. KDoc starts with /** and ends with */; it can hold tags like @param and @return.

Functions with KDoc show up as tooltips in the IDE. So when you use a library, you easily see what a function does.

/**
 * Sums two numbers.
 * @param a the first number
 * @param b the second number
 * @return the sum
 */
fun add(a: Int, b: Int): Int = a + b

Practice Add a comment

Complete this code: a fun main() with a println("Hello Kotlin") inside. Add a single-line comment before println; the comment must start with // and contain a short note.

Sample solution

Main.kt
fun main() {
    // start
    println("Hello Kotlin")
}

Expected output

Hello Kotlin

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which is a single-line comment?

  • # a comment
  • <!-- a comment -->
  • -- a comment
  • // a comment Correct answer

Explanation: A line starting with // is a single-line comment.

2 Complete the comment

Question
println("Hello") ____ a comment
Answer
println("Hello") // a comment

Explanation: // starts a single-line comment.

3 Comments: match the terms

Term Meaning
// Single line
/* */ Block comment
/** */ KDoc
# Invalid

All lessons in Beginner