Table of contents
1.
Introduction
2.
What are Comments in Kotlin?
3.
Types of Comments
3.1.
Single-line comments
3.2.
Multi-line comments
3.3.
Nested comments
4.
FAQs
5.
Key Takeaways  
Last Updated: Mar 27, 2024

Kotlin Comments

Author Vivek Goswami
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

While programming, we often encounter the problem of readability, i.e., it becomes difficult for a user to understand what exactly a function or that line of code is supposed to do when executed. 

The same problem is encountered in the documentation of the programs. It becomes difficult for a programmer to understand the functioning of a particular program just by having a look at it.

One way to solve this problem could be by introducing comments in the program to explain what exactly a program or a function does when executed. 

Now that we have the basic idea of what our problem is and how to solve it let us move forward to knowing what are comments in Kotlin, the different types of comments in Kotlin, and how to use them to increase the readability of the code.

What are Comments in Kotlin?

A comment is a meaningful user-readable explanation of the source code.
The Comments are used in programs with the sole purpose of making the code easier for users to understand, and the compiler ignores them.
Similar to most programming languages, Kotlin also supports single-line and multi-line comments. 
Comments in Kotlin are pretty identical to the comments available in C++ and Java.

Types of Comments

There are mainly two types of comments in Kotlin, just like most programming languages.
Single-line comments are for commenting out a single line of the statement and multi-line comments are for commenting out the multiple lines of statements in the program. 

Single-line comments

As mentioned above, they are used to comment out a single line of statements in a program. In Kotlin, single-line comments start with two forward slashes (//) and end as the line ends. So the compiler will ignore anything that lies between two slashes (//) and the end of the line.

Syntax:

// User-written explanation or statement

We will try to understand better with an example of a code written in Kotlin to display single-line comments.

Example:

// This is a single-line comment to see how they function.
fun main() {
    println("Hey Ninja!") // Printing the text we want to.
}

Output:

Hey Ninja!

In the above example, we saw how the compiler ignored the comment and executed the main function with the println command.

Also, single-line comments can be started from anywhere in the line and will be extended to the end of the line.

Multi-line comments

Multiple line comments are used to comment out multiple lines of statements in a program. In Kotlin, multi-line comments start with a single slash followed by an asterisk(/*) and end. 
All the succeeding lines will have an asterisk at the beginning of the statement. 
At the end of the last statement, an asterisk is followed by a slash (*/).
So the compiler will ignore anything that lies between /* and */.

Syntax:

/* First line of the multi-line comment
Second line of the multi-line comment
Last line of the multi-line comment */

Let us try to understand it better with an example.
Code:

/* Hey Ninja, this is an example of a multi-line comment.
* Here, we'll see how the compiler will ignore this comment.
  Keep Learning!*/

fun main() {
    println("Hey Ninja!")
}

Output

Hey Ninja!

Nested comments

Nested comments are not another type of comments in Kotlin but are a combination of single-line and multi-line comments nested within each other.
We’ll try to understand it better with an example

Code:

/* This is a multi-line comment, and it can have
 * as many lines as the user wants it to have
 /* This is a multi-line comment nested inside a multi-line comment */
 // This is a single-line nested comment.
 */

fun main() {
    println("Hey Ninja!")
}

Output:

Hey Ninja!

In the above example, we saw how comments are nested within each other.

FAQs

  1. Do comments degrade the performance of the programs in Kotlin?
    No, comments do not affect the performance of the Kotlin program as the compiler ignores them.
     
  2. Is there any limit in the length of the comment in single-line comments?
    No, there is no such limit. We can extend the comments till the end of the line.
     
  3. What is the fastest way to comment out a code in Kotlin?
    Whenever writing a comment, just select the code block that you want to comment out and press CTRL+/. It will comment out the selected portion with single-line comments. 

Key Takeaways  

In this blog, we have learned about the usage of comments in Kotlin and their importance. We discussed several types of comments. We also learned to use these different types of comments in the language. To learn more such concepts related to programming and coding do visit our official website.

And also check out our fully-fledged course on Android Development.

Happy learning!! 

Live masterclass