Kotlin Comment Tutorial With Example

In this article, we will learn about kotlin comment. We will learn how to comment line of code in any program in kotlin.

Why do we need comment in program?

The answer is – To explain the functionality of a single line of code , or function or a portion of code.

In comments, we write what does the function do?, why did we write the code in this way (if complex code is written) ? etc.

Note – Whatever you write in the comment is ignored by kotlin compiler.

Like java, There are 2 ways to write comment in kotlin. They are –

A. Single line Kotlin Comment.
B. Multiple Line Kotlin comment.

Single Line Comment in Kotlin

To write single line kotlin comment, you need to use // . For example,

// This is a single comment line

Let’s see a complete example,

fun main(args: Array<String>) {
   val a = 10   // Assigning value 10 to variable a

Here, we have written comment Assigning value 10 to variable a in the program. While executing this program, kotlin compiler completely ignores the comment part.

What will happen when // is removed from the program?
The compiler will throw error because it does not fit according to the syntax of the program.

Try to remove // and run above program. You will get error.

Multiple line comment in kotlin

How would you write multiple lines of comment in kotlin program?
There are 2 ways to do it.
A. Using // – In this case, you will have to write // in each line before writing the comment. For example,

// This is first line of comment
// This is second line of comment
fun main(args: Array<String>) {
   val a = 10   // Assigning value 10 to variable a
   a  = a * a

Notice the comment before main function. This is a valid multiple lines of comment.

However, there a drawback of using // for multiple comment line.

What if you have to write 20 lines of comment? Would you like to write // before each line?

NO! Absolutely NO!.

This is not a good idea of writing multiple lines of comment in kotlin.

So, What’s is good way to write multiple lines of comment in kotlin?

The answer is – Use /* .. */ .

You should use /* .. */ to write multiple lines of comment in kotlin program. For example,

* This is multiple lines of comment
* You can write a long sentences
* in multiple lines in this way
fun main(args: Array<String>) {
   val element = 19

In this case, kotlin compiler ignores everything between /* and */.

Note that comment should start with /* and end with */ .

When should you use comment in kotlin program?

You should use comment when you have written something complex thing in code so that other developers can understand what you have written. It should not be used everywhere in the program like for defining a variable –

var a = 10 // We have defined variable a and assigned a value 10 in it. 

Above example explains bad commenting style. We should not write comment for obvious thing in the program.

var elements = Array(10, { i -> (i * i).toString() })  // contains square of number from 0 to 9 in array

We can write comment for this purposes as it’s not obvious.

Similarly, you can write comment in kotlin program.

That’s end of tutorial on Kotlin Comment.