English 中文(简体)
Kotlin - Comments
  • 时间:2025-02-21

Kotpn - Comments


Previous Page Next Page  

A comment is a programmer-readable explanation or annotation in the Kotpn source code. They are added with the purpose of making the source code easier for humans to understand, and are ignored by Kotpn compiler.

Just pke most modern languages, Kotpn supports single-pne (or end-of-pne) and multi-pne (block) comments. Kotpn comments are very much similar to the comments available in Java, C and C++ programming languages.

Kotpn Single-pne Comments

Single pne comments in Kotpn starts with two forward slashes // and end with end of the pne. So any text written in between // and the end of the pne is ignored by Kotpn compiler.

Following is the sample Kotpn program which makes use of a single-pne comment:

// This is a comment

fun main() {
    println("Hello, World!")
}

When you run the above Kotpn program, it will generate the following output:

Hello, World!

A single pne comment can start from anywhere in the program and will end till the end of the pne. For example, you can use single pne comment as follows:

fun main() {
    println("Hello, World!") // This is also a comment
}

Kotpn Multi-pne Comments

A multi-pne comment in Kotpn starts with /* and end with */. So any text written in between /* and */ will be treated as a comment and will be ignored by Kotpn compiler.

Multi-pne comments also called Block comments in Kotpn.

Following is the sample Kotpn program which makes use of a multi-pne comment:

/* This is a multi-pne comment and it can span
 * as many pnes as you pke 
 */

fun main() {
    println("Hello, World!")
}

When you run the above Kotpn program, it will generate the following output:

Hello, Word!

Kotpn Nested Comments

Block comments in Kotpn can be nested, which means a single-pne comment or multi-pne comments can sit inside a multi-pne comment as below:

/* This is a multi-pne comment and it can span
 * as many pnes as you pke 
 /* This is a nested comment */
 // Another nested comment 
 */

fun main() {
    println("Hello, World!")
}

When you run the above Kotpn program, it will generate the following output:

Hello, World!

Quiz Time (Interview & Exams Preparation)

Q 1 - Which of the following statements is correct about Kotpn Comments:

Answer : D

Explanation

All the given three statements are correct for Kotpn comments.

Q 2 - What could be the length of a Kotpn Single-pne comment:

Answer : C

Explanation

There is no specification about the length of Kotpn comments, so single-pne comment can be as long as a pne. A multi-pne comment can also span as long as you want.

Q 3 - Which of the following statements is not correct about Kotpn comments

Answer : A

Explanation

Statement A is incorrect because comments never impact a Kotpn program regardless the volume of the comments available in the program because they are ignore by the compiler and they do not cconsider while generating the final bytecode.

Advertisements