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.
Practice
Solution
Step 1: Identify single-line comment syntax in Kotlin
Kotlin uses//to start a single-line comment.Step 2: Compare options
// This is a single-line comment uses//, which is correct. /* This is a single-line comment */ is for multi-line comments. <!-- This is a single-line comment --> is HTML style. # This is a single-line comment is used in some scripting languages but not Kotlin.Final Answer:
// This is a single-line comment -> Option CQuick Check:
Single-line comment = // [OK]
- Using /* */ for single-line comments
- Using # or which are not Kotlin comments
- Confusing multi-line and single-line comment syntax
Solution
Step 1: Recall multi-line comment syntax in Kotlin
Kotlin uses/* ... */to write multi-line comments.Step 2: Analyze options
/* This is a multi-line comment */ correctly uses/* ... */. // This is a multi-line comment is single-line comment syntax. /** This is a multi-line comment */ is documentation comment syntax. <!-- This is a multi-line comment --> is HTML comment syntax.Final Answer:
/* This is a multi-line comment */ -> Option AQuick Check:
Multi-line comment = /* ... */ [OK]
- Using // for multi-line comments
- Confusing documentation comments /** */ with regular multi-line comments
- Using HTML style comments
fun main() {
// println("Hello")
println("World")
}Solution
Step 1: Understand comment effect on code execution
The line// println("Hello")is a comment, so it does not run.Step 2: Identify which println runs
Onlyprintln("World")runs, so output is "World".Final Answer:
World -> Option BQuick Check:
Commented code does not run = World [OK]
- Thinking commented println runs
- Expecting both lines to print
- Confusing comment syntax and output
fun main() {
/* This is a comment
println("Hello")
}Solution
Step 1: Check multi-line comment syntax
The comment starts with/*but does not have a closing*/.Step 2: Understand impact of missing closing comment
Without closing*/, the compiler treats the rest as comment, causing syntax error.Final Answer:
Missing closing */ for multi-line comment -> Option DQuick Check:
Multi-line comments must close with */ [OK]
- Forgetting to close multi-line comments
- Confusing single-line and multi-line comment syntax
- Assuming code inside unclosed comment runs
Solution
Step 1: Identify documentation comment syntax in Kotlin
Kotlin uses/** ... */for documentation comments that tools can parse.Step 2: Compare with other comment types
// This is a documentation comment is single-line comment syntax. /* This is a documentation comment */ is regular multi-line comment syntax. <!-- This is a documentation comment --> is HTML comment syntax.Final Answer:
/** This is a documentation comment */ -> Option AQuick Check:
Documentation comments use /** ... */ [OK]
- Using // or /* */ for documentation comments
- Confusing documentation comments with regular comments
- Using HTML comment style by mistake
Ready to prove it?
Pass the test and this pattern moves unknown
