Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: April 7, 2025
KDoc is the official format for documenting Kotlin code. It’s an extension to Javadoc that supports Kotlin’s specific constructs.
In this tutorial, we’ll explore the fundamentals of KDoc, its benefits, and how to use it effectively. We’ll also look at some of the different ways to use KDoc to document Kotlin code.
Fundamentally, KDoc merges Javadoc’s block tag syntax with Markdown’s inline markup approach.
Specific KDoc syntax is recognized by the Kotlin compiler which, along with Dokka, may be used to generate documentation in various formats. Most importantly, following KDoc’s syntax convention allows for a concise and apprehensible codebase by improving readability and enhancing collaboration between developers.
Similar to Javadoc, KDoc comments are enclosed by /** and */ constructs and can have an asterisk at the beginning of each line without being treated as part of the comment’s content. As per convention, the initial paragraph of the documentation text until the first empty line serves as the summary description of the element, while the subsequent text provides a detailed description.
Let’s look at a documentation example of a Blog class that has a single method for storing an article:
/**
* A blog of *articles*.
*
* This class is just a **documentation example**.
*
* @param T the type of article in this blog.
* @property name the name of this blog.
* @constructor creates an empty blog.
* @see com.baeldung.kdoc.TitledArticle
* @sample com.baeldung.kdoc.blogSample
*/
class Blog<T>(private val name: String) {
private var articles: MutableList<T> = mutableListOf()
/**
* Adds an [article] to this blog.
* @return the new number of articles of the blog.
*/
fun add(article: T): Int {
articles.add(article)
return articles.size
}
}
/**
* An article with a title.
*
* @property title the title of this article.
* @constructor creates an article with a title.
*/
data class TitledArticle(private val title: String)
private fun blogSample() {
val blog = Blog<TitledArticle>("Baeldung on Kotlin")
val blogCount = blog.add(TitledArticle("An Introduction to KDoc"))
}
Notice that we can document both the class itself and its methods. The documentation also includes Markup elements for styling our documentation.
KDoc block tags start with the “@” symbol, followed by the tag name, and are placed above the code elements they describe. Let’s go through the most commonly used KDoc block tags:
In this article, we’ve given an overview of the KDoc format for documenting Kotlin code. By combining Javadoc’s block tag syntax with Markdown’s inline markup approach, KDoc offers a concise and structured documentation format that is both developer-friendly and compiler-recognized.
With a wide range of block tags, KDoc enables comprehensive documentation that includes information about parameters, return values, exceptions, receivers, and more. It empowers us to write well-documented and maintainable code, which is accessible to both current and future contributors.