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

Comments are statements that are used for documentation purposes. Comments are disregarded by the compiler so that don’t execute. We can likewise use it for providing information about the line of code.

In programming, comments are a portion of the program planned for you and your fellow programmers to understand the code. They are totally overlooked by the Kotlin compiler (Kompiler).

Comparable like Java, there are two sorts of comments in Kotlin

  • /* … */
  • // ….

Traditional comment/* … */

This is a multiline comment that can span over various lines. The Kotlin compiler overlooks everything from/* to */. For instance,

/* 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 likewise used for documenting Kotlin code (KDoc) with a little variety. The KDoc comments begin with/** and close with */.


End of Line Comment //

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

// 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

and

// outputs Hello, World! on the screen

Use Comments the Right Way

Comments shouldn’t be the substitute for an approach to clarify ineffectively composed code in English. Compose all around organized and meaningful code, and afterward use comments.

There isn’t anything amiss with using comments to clarify complex algorithms, regex, or scenarios where you have picked one procedure over another (for future reference) to tackle the issue.

By and large, use comments to clarify ‘why’ as opposed to ‘how’ and you are all set.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.


0 Comments

Leave a Reply

Avatar placeholder