Comments & Javadoc
Comments & Javadoc
Code is written once but read many times — by your future self, by teammates, and by tools. Comments and Javadoc are the primary way Java developers communicate intent inside source files. In this lesson you will learn every comment style Java supports, when to use each one, and how to write professional API documentation with Javadoc tags.
Line Comments
A line comment starts with // and runs to the end of the line. Use it to explain a single statement or to leave a short note.
Line comments are ideal for brief, local explanations. Keep them short — if a comment needs multiple sentences, a block comment is usually cleaner.
Block Comments
A block comment opens with /* and closes with */. Everything between those delimiters is ignored by the compiler, regardless of how many lines it spans.
/* ... */, test, then remove the comment. Just be careful not to leave commented-out code in production — version control is the right place for old code.
Javadoc Comments
Javadoc comments start with /** (two asterisks) and close with */. They are placed directly above a class, interface, constructor, method, or field declaration. The javadoc tool reads these comments and generates HTML documentation — the same HTML you see on the official Java API site (docs.oracle.com).
Common Javadoc Tags
Tags always appear inside a /** ... */ block and start with @. The most important ones are:
@param name description— documents one method parameter.@return description— describes the return value (omit forvoidmethods).@throws ExceptionType description— documents a checked or runtime exception the method can throw.@author name— records the author (class-level only).@version text— records the version (class-level only).@since text— notes the Java version or product version this API was introduced.{@code expression}— inline tag; renders as monospace without HTML-processing the content.{@link ClassName#method}— creates a clickable cross-reference in the generated HTML.
public and protected members that other developers will call.
Writing Self-Documenting Code
The best comment is often no comment at all — because the code itself is clear enough. Self-documenting code uses meaningful names, small focused methods, and well-chosen constants so that a reader can understand the intent without stopping to read a comment.
Similarly, a method named calculateMonthlyInterest(double principal, double annualRate) tells you everything without a comment. A method named calc(double a, double b) forces you to read its body.
Generating the HTML Docs
Run the javadoc command from the terminal to turn your comments into a browsable website:
The -d docs flag sets the output directory. Open docs/index.html in a browser to see your formatted documentation. IDEs like IntelliJ IDEA also show Javadoc inline in tooltips as you type.
Summary
Use // for short one-line notes, /* ... */ for multi-line explanations or temporary code removal, and /** ... */ Javadoc for all public API members. Invest in descriptive names so your code reads like prose, and reserve comments for explaining why a decision was made — not what the code does. Good documentation is a mark of professional Java development.