Comments are notes you leave in your code for yourself and others. The computer ignores them completely, but they make your code way easier to understand later.
Single-line comments with //
Everything after // on a line is treated as a comment.
fun main() {
// This is a single-line comment
println("Comments are invisible to the machine")
// println("This line won't run because it's commented out")
}
Multi-line comments with /* */
Use these when you need more than one line of explanation.
fun main() {
/*
This is a multi-line comment.
You can write as much as you want here.
None of it will affect the program.
*/
println("See? No problem.")
}
KDoc — documenting your code /** */
For functions and classes, Kotlin has a special documentation comment format called KDoc. It starts with /** and uses @param and @return tags to describe what your code does.
/**
* Calculates the square of a number.
* @param x the number to square
* @return x multiplied by itself
*/
fun square(x: Int): Int {
return x * x
}
fun main() {
println(square(5))
}
Most IDEs will show these docs when you hover over the function name.
Try it Yourself →