Javadoc &
Comments
Learn to write Javadoc and comments that explain the intent behind your code: not what it does, but why it does it.
Why Comments Matter
Code is read far more often than it is written. Six months from now, you (or a teammate) will re-open your file and need to understand why a decision was made: not just what the syntax does.
Java gives you three distinct tools for this job. Each has a different audience and purpose. Mastering them means your code communicates on every level.
The Three Comment Types
Anatomy of a Javadoc Comment
The Core Rule
i++;
i++;
The first comment is pure noise: any Java reader knows i++ increments. The second comment reveals intent: we skip a row because it is a header.
Essential Javadoc Tags
| Tag | Purpose | Placement |
|---|---|---|
| @param | Describes a method parameter | Methods, constructors |
| @return | Describes the return value | Non-void methods |
| @throws | Documents an exception thrown | Methods that throw |
| @author | Names the author | Classes, interfaces |
| @version | Version number of the class | Classes |
| @see | Cross-reference to related class/method | Anywhere |
| @deprecated | Marks element as outdated | Any documented element |
Javadoc vs Implementation Comments
| Aspect | Javadoc /** */ | Implementation // or /* */ |
|---|---|---|
| Audience | Users of your API | Developers reading the source |
| Placement | Before class, method, or field | Inside method bodies |
| Tool support | Parsed by javadoc tool | Ignored by all tools |
| Focus | Contract: what, params, return | Intent: why this approach |
| Appears in IDE | Hover tooltips, autocomplete | Only when viewing source |
Step-by-Step Writing Process
Example 1: Basic Method
Example 2: Handling Edge Cases
Example 3: Class-Level Javadoc
When to Write Which Comment
Writing Great Summary Lines
getName() documented as "Gets the name" adds zero information. Say what name and why it matters.Inline Comments: Less Is More
Inline comments (//) are powerful when used sparingly. Reserve them for lines where the reasoning is invisible from the code alone.
Common Mistakes
// set x to 5 above x = 5; adds nothing. Describe intent, not mechanics.Quick Reference Summary
| Question | Answer |
|---|---|
| When to use /** | Before every public class, method, constructor, and field |
| When to use // | Inside method bodies where the why is non-obvious |
| When to use /* */ | Multi-line internal notes, algorithm explanations, or license headers |
| What to comment | Intent, constraints, edge cases, design trade-offs |
| What NOT to comment | What the code literally does, obvious getter/setter logic |
| Summary line style | Third-person verb phrase, one sentence, ends with a period |
| Maintenance rule | Update the comment every time you change the code |
Question 1 of 5
Which comment type is parsed by the javadoc tool to generate HTML documentation?
Question 2 of 5
Code snippet:
// increment count by 1
count++;
What is wrong with the comment above?
Question 3 of 5
Which Javadoc tag should you use to document a method parameter?
Question 4 of 5
A Javadoc summary line should begin with which of the following?
Question 5 of 5
Scenario: You have a private helper method sanitizeInput(String raw) inside your class. It contains a regex workaround for a known Java bug.
What is the best commenting strategy for this method?