KDoc Comments in Kotlin
On this page (12sections)
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 + b
KDoc 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, Kotlin
When 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
- Understand the core idea: kDoc is Kotlin’s documentation comment format, similar to Javadoc.
- Prefer readable names and small functions so examples map directly to real projects.
- Run and modify the example — change values and observe how the output changes.
Common Mistakes
- Skipping the example and only reading the definition — hands-on practice cements the concept.
- Copying syntax without understanding nullable vs non-nullable types or scope rules.
- Ignoring compiler warnings that often point to safer alternatives.
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.