Kotlin Comments Tutorial

In this section, we will learn what comments are and how to use them in Kotlin.

What is comment in Kotlin?

In programming, source codes can quickly get lengthy and up to thousands of lines of codes. It’s literally impossible to remember all the works and algorithms you’ve written in a source code! And of course it’s really important to remember the structure of your code so that in the future when backed to maintain the source code or even enhance it, you’d be able to correctly modify the code without producing bugs.

This is where we can use the comments in Kotlin!

Comments are a way of explaining the work of a source code within the source code itself!

For example, let’s say you have a lengthy function and now you want to explain what the job of this function is so that in the future when you returned to it, be able to read the explanations quickly and get to know everything in a short time.

This is where comments can help us tremendously.

There are other reasons of why we use comments as well:

For example, let’s say you’re working on a project with someone else. You’ve written part of the code and now pass it to your colleague to work on the rest of the code. Here you can use comments to explain your work in the source code and so the other person can open the source and see how things are done with the help of comments you’ve provided.

Another example would be when you’re reviewing a source code of a colleague! For example, you’re in charge of checking to see if their source code is written properly or not. Here you can use comments and write any necessary opinion in the source code itself so that other people can exactly see what and where there are issues in the source code.

Note: comments are for humans and not the compilers. So when we have a source code with comments, the compiler will remove those comments from the source code and compile the rest.

Types of Comments in Kotlin

in short, there are two types of comments in Kotlin:

  • Single line comment
  • Multiline comment

Kotlin Single Line Comment

This type of comment, as the name suggests, is used to create a comment that is as short as just one single line.

We use this type of commenting when the length of our comment is short between one or two lines long.

Kotlin Single Line Comment Syntax:

// comment…

Simply put two forward-slashes with no space in between and then write the comment afterward.

Note that a single line comment starts after the two forward slashes and it includes the entire line after that. So if you have a source code or something in that line, it will be part of the comment and the compiler will ignore it.

Example: creating single line comment in Kotlin

// This is a single line of comment
fun main(){
    var name = "John"
    var secondName = name 
    // we can use single line comment inside and outside of functions. 
    println(name)
    println(secondName)

    println("***")

    secondName = "Jack" // a single line comment can appear after a statement as well. 

    println(name)
    println(secondName)
}

Kotlin Multiple Lines Comment

On the other hand, a multiline comment is used when we want to write a comment that extends multiple lines.

Kotlin Multiple Lines Comment Syntax:

/*

comments…

*/

A multi-line comment starts with a forward slash followed by * and ends with another * followed by a forward slash.

Example: creating multiple lines comment in Kotlin

/*
This is a 
multiline 
comment
*/
fun main(){
    var name = "John"
    var secondName = name 
    /*
    A multiline comment 
    can 
    appear 
    inside and outside 
    of a 
    function too. 
    */
    println(name)
    println(secondName)

    println("***")

    secondName = "Jack"

    println(name)
    println(secondName)
}

Note: sometimes we want to remove part of a source code temporarily in order to test something, for example; in situation like this, we can surround the target codes with comments (AKA commenting out those codes) which practically has the same effect as if we’ve removed the target codes (again this is because compilers automatically remove the comments of a source code before compiling it). So this way we can make the compiler to skip the parts of a source code without actually removing them.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies