A comment is like a brief note or piece of text in a program for the programmers. Comments are usually for the programmers to make the code readable. While a program is compiled and run, it is ignored by the compiler. Comments make a code more readable and understandable. Like in any other modern programming language, there are two types of comments in Kotlin as well – single line comments, also known as end-of-line comments and multi-line comments, also known as block comments.
Single line comments are indicated by two forward slashes (//). On the other hand, the opening of a multi-line comment is indicated by a forward slash(/) followed by an asterisk(*) and the ending by an asterisk(*) followed by a forward slash(/). Let’s see the example given below:
fun main(){
println(2+2) //This a single-line comment
/* This is a multi-line comment.
* Add as many lines as you want!
*
* Everything between /* and */ will
* be considered as a comment
*/
println("Bye!")
}
As you can see in the output, no matter what you write after // in a line or between /* and */, everything will be ignored by the Kotlin compiler, and only the rest of the code will be executed.
- Nested Comments: We can write a single-line or a multi-line comment inside another multi-line comment too. Such comments are referred to as nested comments. Let’s try it out on the console as shown in the example below:
fun main(){
println("Hi!") //This a single-line comment
/* Multi-line comment starts.
* //This is a nested comment
*///Multi line comment ends.
println("Bye!")
}
Keywords
Kotlin has a set of pre-reserved words with special meanings to the compiler, known as keywords. For example, fun, class, set, etc. Keywords are restricted from being used as identifiers (names given to variables, functions, classes, or packages). Since Kotlin is a case-sensitive language, you can name an identifier similar to that of a keyword with a changed case. It is recommended not to use keyword names as identifier names though.
Kotlin keywords are classified into three kinds based on their usage as identifiers–
- Hard Keywords – The keywords in Kotlin that are strictly prohibited to be used as identifiers are called hard keywords. In case a hard keyword is used as an identifier, the compiler raises an error. Example: as, as?, break, class, fun, for, etc. there are a total 31 hard keywords in Kotlin.
- Soft Keywords – The keywords in Kotlin that serve as keywords in one context but can be used as identifier names in other contexts are referred to as soft keywords. Example: by, catch, file, init, etc. Kotlin has 18 soft keywords in total.
- Modifier Keywords: The tokens that fulfil the purpose of keywords in the modifiers lists of declarations but can be used as identifiers otherwise are known as modifier keywords. 21 modifier keywords include Kotlin include const, except, inline, out, etc.