Kotlin Comments

In programming, comments are portion of the program intended for you and your fellow programmers to understand the code. They are completely ignored by the Kotlin compiler (Kompiler).

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

  • /* ... */
  • // ....

Traditional comment /* ... */

This is a multiline comment that can span over multiple lines. The Kotlin compiler ignores everything from /* to */. For example,

/* This is a multi-line comment.
 * The problem prints "Hello, World!" to the standard output.
fun main(args: Array<String>) {

   println("Hello, World!")

The tradition comment is also used for documenting Kotlin code (KDoc) with a little variation. The KDoc comments starts with /** and ends with */.

End of Line Comment //

The compiler ignores everything from // to the end of the line. For example,

// Kotlin Hello World Program
fun main(args: Array<String>) {

   println("Hello, World!")      // outputs Hello, World! on the screen

The program above contains two end of line comments:

// Kotlin Hello World Program


// outputs Hello, World! on the screen

Use Comments the Right Way

Comments shouldn't be the substitute for a way to explain poorly written code in English. Write well structured and readable code, and then use comments.

Some believe that code should be self-documenting and comments should be scarce. However, I have to disagree with it completely (It's my personal opinion). There is nothing wrong with using comments to explain complex algorithms, regex or scenarios where you have chosen one technique over other (for future reference) to solve the problem.

In most cases, use comments to explain 'why' rather than 'how' and you are good to go.

Did you find this article helpful?