English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Kotlin Comments

In this article, you will learn about Kotlin comments, as well as why and how to use them.

In programming, comments are part of the program, intended for you and other programmers to understand the code. The Kotlin compiler (Kompiler) completely ignores them.

Similar to Java, there are two types of comments in Kotlin

  • /* ... */

  • // ....

Traditional comments / * ... * /

This is a multi-line comment that can span multiple lines. The Kotlin compiler ignores them from /*  to  */All content. For example,

/* This is a multi-line comment.
 * Prints 'Hello, World!' to standard output.
 */
fun main(args: Array<String>) {
   println("Hello, World!")
}

Traditional comments are also used for documenting Kotlin code (KDoc) and have slight changes. KDoc comments are denoted by/ **beginning, followed by* /end.

end of line. //

The compiler ignores comments from // All content to the end of the line... For example,

// Kotlin 'Hello World' Program
fun main(args: Array<String>) {
   println("Hello, World!")      // Output 'Hello, World!' to the screen
}

The above program contains two end-of-line comments:

// Kotlin 'Hello World' Program

and

// Output 'Hello, World!' to the screen

Correct Use of Comments

Comments should not replace the method of explaining poorly written code in English. Write well-structured and readable code, and then use comments.

In most cases, comments are used to explain 'why' rather than 'how', just follow this rule.