How to write comments in Kotlin language? Comments contain a description, explain single or multiple lines of a code
A developer can better understand the code by reading the comments. These are ignored at runtime.
In Kotlin, You can write a comment in different following ways.
- Single-line comments
- Multi-Line comments
- Documentation commands
Kotlin comments are similar to Java Comments. It is always good practice to add Comments to the below blocks or lines of code.
- Classes description
- before the method declaration
- Any complex logic code
- documentation comments
Single Line comments in Kotlin
These are also called inline comments.
- It always starts with double forward slashes (// ) symbol characters and ends with a line break.
- It is a description or piece of text for a single line of code.
- These comments are not required at the start at the beginning of the line but also be written in the middle or end of the line Syntax:
// This is single line comments in Kotline Code
Example
hello.kt:
// Hello World application
println("Hello World application")
Learned single-line comments, How do you write multi-line comments?
Multi-line comments in Kotlin
These comments are written in multiple lines and are also called block comments.
Here is a way we can write a multi-line comment.
Multi-line Comments always start with /*
and are followed by single or multiple lines and end with */
/* multi line comments 1
multi-line comments 2
multi-line comments 3 */
You can also write multi-line comments with single-line comment syntax as given below.
// multi line comments 1
// multi line comments 2
// multi line comments 3
This allows you to write multiple lines of code and be ignored by the compiler.
Block level comments in Kotlin Example:
// Hello world sample program
// Basic program to print hello world to the console
/* First code to write in Kotlin programming language */
print("Hello World app");
Conclusion
You learned different types of comments in the Kotlin programming language.
- single line comments (
//
) - Multiple line comments (Starting line begins with
/*
and ending line ends with*/
)