Kotlin – Comments

Kotlin comments

In this guide we will discuss about comments in Kotlin. A comment is a programmer-readable explanation or annotation in the Kotlin source code. They are added with the purpose of making the source code easier for humans to understand, and are ignored by Kotlin compiler.

Just like most modern languages, It supports single-line (or end-of-line) and multi-line (block) comments. Kotlin comments are very much similar to the comments available in Java, C and C++ programming languages.

Single-line Comments

Single line comments in Kotlin starts with two forward slashes // and end with end of the line. So any text written in between // and the end of the line is ignored by Kotlin compiler.

Following is the sample Kotlin program which makes use of a single-line comment:

// This is a comment

fun main() {
    println("Hello, adglob!")
}

When you run the above Kotlin program, it will generate the following output:

Hello, adglob!

A single line comment can start from anywhere in the program and will end till the end of the line. For example, you can use single line comment as follows:

fun main() { 
     println("Hello, adglob!") // This is also a comment
 }

Multi-line Comments

A multi-line comment in Kotlin starts with /* and end with */. So any text written in between /* and */ will be treated as a comment and will be ignored by Kotlin compiler.

Multi-line comments also called Block comments in Kotlin.

Following is the sample Kotlin program which makes use of a multi-line comment:

/* This is a multi-line comment and it can span
 * as many lines as you like 
 */

fun main() {
    println("Hello, adglob!")
}

When you run the above Kotlin program, it will generate the following output:

Hello, adglob!

Kotlin Nested Comments

Block comments in Kotlin can be nested, which means a single-line comment or multi-line comments can sit inside a multi-line comment as below:

/* This is a multi-line comment and it can span
 * as many lines as you like 
 /* This is a nested comment */
 // Another nested comment 
 */

fun main() {
    println("Hello, adglob!")
}

When you run the above Kotlin program, it will generate the following output:

Hello, adglob!

Next Topic : Click Here

This Post Has 3 Comments

Leave a Reply