Writing
Javadoc
Learn to write professional documentation for your Java methods using Javadoc comments: the standard that powers every Java library you use.
What is Javadoc?
Javadoc is Java's built-in documentation system. It lets you embed structured descriptions directly above your classes, methods, and fields using special comment blocks. The Java compiler can then generate HTML reference pages automatically from these comments.
Anatomy of a Javadoc Comment
Why Write Javadoc?
The Three Essential Tags
The Javadoc Structure
Javadoc vs Regular Comments
| Javadoc Comment | Regular Comment | |
|---|---|---|
| Syntax | /** ... */ | // or /* ... */ |
| Audience | Users of your code | Developers reading source |
| Parsed by tools | ✓ Yes | ✕ No |
| Supports tags | ✓ @param, @return | ✕ No |
| IDE integration | ✓ Tooltips | ✕ Ignored |
| Placement | Before class/method | Anywhere |
How to Write Javadoc: Step by Step
/** and press Enter. Most IDEs auto-generate the skeleton with @param and @return stubs.Example 1: Simple Method
Example 2: Multiple Parameters
Example 3: Void Method
Good vs Bad Descriptions
| ✕ Bad | ✓ Good | |
|---|---|---|
| Description | "This method does stuff" | "Calculates the area of a rectangle" |
| @param | "@param x the x" | "@param width the width in pixels" |
| @return | "@return the result" | "@return the area as a double" |
| First word | "This method calculates..." | "Calculates..." |
| Specificity | "Takes a number" | "the radius in centimeters" |
Common Javadoc Tags
Common Mistakes
Javadoc Checklist
Question 1 of 5
Which opening delimiter marks a comment as a Javadoc comment (as opposed to a regular block comment)?
Question 2 of 5
Consider this method:
public int findMax(int a, int b) {
return (a > b) ? a : b;
}
How many @param tags should the Javadoc for this method contain?
Question 3 of 5
When should you omit the @return tag from a Javadoc comment?
Question 4 of 5
Which of the following is the best first sentence for a Javadoc description?
Question 5 of 5
Consider this Javadoc comment:
/*
* Calculates the average of two numbers.
*
* @param a the first number
* @param b the second number
* @return the average as a double
*/
public double average(double a, double b) {
return (a + b) / 2.0;
}
What is wrong with this Javadoc comment?