KDoc Comments in Kotlin
Introduction
KDoc Comments is a fundamental concept every Kotlin developer should understand. Start here with your first program, variables, comments and KDoc documentation basics.
KDoc is Kotlin’s documentation comment format, similar to Javadoc. In this tutorial you will learn the syntax, walk through a complete example program, study the sample output, and review best practices so you can apply the concept confidently in your own projects.
Definition
- KDoc is Kotlin’s documentation comment format, similar to Javadoc.
- KDoc comments start with /** and end with */ and support Markdown syntax.
- Documentation tools and IDEs use KDoc to show inline help for functions and classes.
Syntax
/**
* Returns the sum of two integers.
* @param a first number
* @param b second number
*/
fun add(a: Int, b: Int): Int = a + bKDoc Comments in Kotlin Example Program in Kotlin
/**
* KDoc example program
*/
fun greet(name: String): String = "Hello, $name"
fun main() {
println(greet("Kotlin"))
}Sample Output
Hello, KotlinWhen to use
Start with basic programs before moving to classes, collections, or coroutines.
How it works
-
The program starts with a
mainfunction — the entry point that runs when you execute the file. -
KDoc is Kotlin’s documentation comment format, similar to Javadoc.
-
Run the program in IntelliJ IDEA, Android Studio, or with the Kotlin command-line compiler (
kotlinc/kotlin). Compare your console output with the sample output shown below.
Best Practices
Common Mistakes
Key Points
- KDoc is Kotlin’s documentation comment format, similar to Javadoc.
- KDoc comments start with /** and end with */ and support Markdown syntax.
- Documentation tools and IDEs use KDoc to show inline help for functions and classes.
- Test the example locally and verify the output matches the sample.
- Experiment by changing input values to see how behaviour changes.
Notes
- Semicolons at the end of statements are optional in Kotlin.