Kotlin Comments

Kotlin Tutorial

 Kotlin Comments – Complete Guide

Comments in Kotlin are used to explain code, make it readable, and prevent execution of certain lines.
They are ignored by the compiler.


 Why Use Comments?

  •  Explain logic
  •  Improve readability
  •  Help in debugging
  •  Useful for teamwork
  •  Documentation purpose

 Types of Comments in Kotlin

Kotlin supports three types of comments:

  •  Single-Line Comments
  •  Multi-Line Comments
  •  Documentation Comments (KDoc)

 1. Single-Line Comment

Used for short explanations.

  •  Starts with //
  •  Ends at line break

 2. Multi-Line Comment

Used for long explanations.

  •  Starts with /*
  •  Ends with */

 Nested Comments (Special Feature)

Kotlin supports nested multi-line comments.

  • Java does NOT support this, Kotlin does!

 3. Documentation Comment (KDoc)

Used to generate documentation.

  •  Starts with /** 
  •  Used with classes, functions, properties

 Common KDoc Tags

TagPurpose
@paramDescribes parameter
@returnDescribes return value
@propertyClass property
@constructorConstructor
@throwsException info

 Commenting Out Code (Debugging)


 

  •  Useful during testing
  •  Don’t leave commented code in production

 Comments vs Code

  •  Over-commenting
  •  Explaining obvious code
  •  Outdated comments

 Write clean code first, comments second


Best Practices

  •  Comment why, not what
  •  Use KDoc for public APIs
  •  Keep comments updated
  •  Avoid unnecessary comments

Interview Questions – Kotlin Comment

Q1. How many types of comment in Kotlin?
Three.

Q2. Does Kotlin support nested comment?
Yes.

Q3. What is KDoc?
Kotlin documentation comment system.

Q4. Are comments executed?
No.


 Summary

  • // → Single-line comment
  • /* */ → Multi-line comment
  • /** */ → Documentation (KDoc)
  •  Kotlin supports nested comment
  •  Comment improve readability

You may also like...