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
-
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.
-
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.
-
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!!