Comment is an important part of your program, it improves the readability of your code and makes it easier to understand. Writing meaningful comments in the code is considered a good programming practice. Similar to Java, Kotlin also supports single line and multi-line comments. The syntax of comments in Kotlin is same as Java.
What is a comment?
A comment is a meaningful text written by the programmer in the various places of the code to explain the purpose and logic of the code. Comments are written to improve the readability of the code, they play no role in the compilation and execution of the program and they are completely ignored by the compiler during the compilation of the program.
Types of Comments in Kotlin
There are two types of comments in Kotlin – 1) Single line comment 2) Multiple line comment
1) Single line comment
As the name suggests, this type of comment is to write a single line of comment. Single line comments are started with the double slash, any text written after double slash is ignored by the compiler.
fun main(args: Array<String>) { //This is a single line comment println("Hello World!") }
The text after double slash “This is a single line comment” is a comment. I have written this text to demonstrate the use of comment, However the text should explain what is the purpose of following line or block of the code, so the meaningful comment should look like this:
fun main(args: Array<String>) { //This displays the "Hello World!" message on the output screen println("Hello World!") }
2) Multi-line comment
There are cases where we need to explain the piece of code in several lines, in such cases we use the multi-line comments. We start the comment with /*
and ends the comment with */
.
For example:
fun main(args: Array<String>) { /* This is a multi-line comment. I'm writing this * text to show you that we can write the comments * in multiple lines */ println("Hello World!") }
Leave a Reply