BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Kotlin Comments

By Chaitanya Singh | Filed Under: Kotlin Tutorial

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
kotlin comments

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!")
}
❮ PreviousNext ❯

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Kotlin Tutorial

  • Kotlin Tutorial
  • Kotlin in Eclipse IDE
  • Kotlin in IntelliJ
  • First Kotlin Program
  • Kotlin Keywords
  • Kotlin Variables
  • Kotlin Type Casting
  • Kotlin operators
  • Kotlin Input/Output
  • Kotlin Comments

Kotlin String

  • Kotlin String

Kotlin Array

  • Kotlin Array
  • Kotlin Range

Control Flow

  • Kotlin if expression
  • Kotlin when expression
  • Kotlin for loop
  • Kotlin while loop
  • Kotlin do-while loop
  • Kotlin continue
  • Kotlin break

Kotlin Function

  • Kotlin Function
  • Kotlin Recursion
  • Kotlin default and named arguments
  • Lambda functions
  • Higher Order Function

Exception Handling

  • Exception Handling
  • Kotlin try catch
  • Multiple catch blocks
  • Nested try catch
  • Throw keyword
  • Kotlin try expression

Kotlin OOP

  • Class and Objects
  • Kotlin Constructors
  • Kotlin Inheritance
  • Visibility Modifiers
  • Kotlin abstract class
  • Kotlin Interfaces
  • Nested and Inner Class
  • Kotlin Data Class
  • Kotlin Sealed Class

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap