Comments in Java – Single-line, Multi-line and Javadoc
Comments are notes in your source code that the compiler ignores. They explain why code exists, warn about tricky parts, and, in the case of Javadoc comments, become the official documentation of your classes and methods.
Java has three kinds of comments: single-line, multi-line and documentation (Javadoc) comments. This tutorial shows each one, the most useful Javadoc tags, how to generate HTML documentation, and how to write comments that actually help.
1. The Three Types of Comments
| Type | Syntax | Used for |
|---|---|---|
| Single-line | // text | Short notes, end-of-line remarks, temporarily disabling a line |
| Multi-line (block) | /* text */ | Longer explanations inside code |
| Documentation (Javadoc) | /** text */ | Describing classes, methods and fields for API documentation |
/**
* Demonstrates the three kinds of comments in Java.
*/
public class CommentTypes {
public static void main(String[] args) {
// Single-line comment: everything after // is ignored
int width = 8; // comments can also follow code on the same line
/*
* Multi-line comment: useful for longer explanations
* that span several lines.
*/
int height = 5;
int area = width * height /* inline block comment */ ;
System.out.println("Area: " + area);
// System.out.println("This line is commented out and will not run");
}
}
Area: 40
The commented-out println did not run, and the block comment in the middle of the expression was simply ignored.
2. Single-Line Comments
A single-line comment starts with // and runs to the end of the line. It is the most common kind of comment:
// Retry three times because the payment gateway sometimes times out
int maxRetries = 3;
double total = price * quantity; // tax is added later in checkout()
3. Multi-Line Comments
A multi-line comment starts with /* and ends with */. Everything between, across any number of lines, is ignored. The leading * on each line is only a style convention.
/*
* Prices are stored in paise (1 rupee = 100 paise) to avoid
* floating-point rounding errors. Convert to rupees only
* when displaying values to the user.
*/
long priceInPaise = 49_950;
/* a /* b */ c */, the comment ends at the first */, and c */ becomes a syntax error.4. Documentation Comments (Javadoc)
A Javadoc comment starts with /** and is placed directly before a class, interface, constructor, method or field. The javadoc tool turns these comments into HTML pages, the same format as the official Java API documentation, and IDEs show them as tooltips.
/**
* A simple calculator for basic arithmetic.
*
* <p>All methods are static, so no object is needed:
* {@code Calculator.divide(10, 2)}.</p>
*
* @author JavaByTechie
* @since 1.0
*/
public class Calculator {
/**
* Divides one integer by another.
*
* @param dividend the number to divide
* @param divisor the number to divide by; must not be zero
* @return the whole-number quotient
* @throws ArithmeticException if {@code divisor} is zero
* @see Math#floorDiv(int, int)
*/
public static int divide(int dividend, int divisor) {
if (divisor == 0) {
throw new ArithmeticException("divisor must not be zero");
}
return dividend / divisor;
}
public static void main(String[] args) {
System.out.println("17 / 5 = " + divide(17, 5));
}
}
17 / 5 = 3
The first sentence becomes the summary shown in method lists, so make it a complete, clear description.
5. Common Javadoc Tags
| Tag | Meaning |
|---|---|
@param name description | Describes a method or constructor parameter. |
@return description | Describes the return value. |
@throws Type description | Describes an exception the method may throw (@exception is a synonym). |
@see reference | Adds a "See Also" link to another class or method. |
@since version | The version in which the element was added. |
@deprecated explanation | Marks the element as deprecated and says what to use instead (use with the @Deprecated annotation). |
@author name | The author of a class. |
{@code text} | Formats text as code without interpreting HTML. |
{@link Class#method} | An inline link to another element. |
{@inheritDoc} | Copies documentation from the overridden method. |
{@snippet : ...} | Embeds a code example (Java 18+). |
6. Generating HTML Documentation
The JDK's javadoc tool reads your source files and produces a set of HTML pages:
javadoc -d docs Calculator.java # writes docs/index.html, docs/Calculator.html ...
# With Maven
mvn javadoc:javadoc
Javadoc warns about public elements that have no comment, which helps you keep a library's API fully documented. Since Java 23, you can also write documentation comments in Markdown using /// lines; see Java Version Improvements and Java 25 Updates.
7. Java Version Improvements and Java 25 Updates
The three comment forms have not changed since Java 1.0. Improvements in recent releases concern documentation comments and the javadoc tool:
| Feature | Java version | Status in Java 25 | What it adds |
|---|---|---|---|
{@snippet} tag | Java 18 | Standard | Code examples in documentation that can be checked and highlighted. |
Markdown documentation comments (///) | Java 23 | Standard | Write documentation in Markdown (CommonMark) instead of HTML. |
Java 25 adds no new comment syntax, so everything on this page applies to Java 25.
Markdown Documentation Comments (Java 23+)
A Markdown documentation comment is a block of consecutive lines that each start with ///. You write lists, `code` and emphasis in Markdown instead of HTML tags such as <ul> and <code>, and Javadoc tags such as @param and @return still work:
/// Converts temperatures between Celsius and Fahrenheit.
///
/// Uses the formula `F = C × 9/5 + 32`.
public class TemperatureConverter {
private TemperatureConverter() { } // utility class: no objects needed
/// Converts a Celsius value to Fahrenheit.
///
/// - Works for negative values, such as `-40`.
/// - The result is not rounded.
///
/// @param celsius the temperature in degrees Celsius
/// @return the same temperature in degrees Fahrenheit
public static double toFahrenheit(double celsius) {
return celsius * 9 / 5 + 32;
}
/// Prints a few sample conversions.
///
/// @param args not used
public static void main(String[] args) {
System.out.println("37.0 C = " + toFahrenheit(37.0) + " F");
System.out.println("-40.0 C = " + toFahrenheit(-40.0) + " F");
}
}
37.0 C = 98.6 F -40.0 C = -40.0 F
javadoc -d docs TemperatureConverter.java
The compiler treats /// lines as ordinary single-line comments, so they do not change the program. The javadoc tool (JDK 23 or later) turns them into the same HTML as a /** ... */ comment: the bullet points become a list, and @param and @return appear as the Parameters and Returns sections. The private constructor is there because this is a utility class; without it, javadoc warns that the default constructor has no comment.
8. A Surprising Detail: Unicode Escapes in Comments
The compiler translates Unicode escapes such as \u000d (carriage return) before it removes comments. A line break hidden in a comment therefore ends the comment early, and the rest of the line is compiled as code:
public class UnicodeInComment {
public static void main(String[] args) {
// Unicode escapes are processed BEFORE comments are removed.
// \u000d System.out.println("Surprise: this line runs!");
System.out.println("Done");
}
}
Surprise: this line runs! Done
This is a popular interview puzzle. In real code, never put Unicode escapes for line breaks, quotes or backslashes in comments.
9. Writing Good Comments
- Explain why, not what. The code already shows what happens; the comment should give the reason, the business rule or the trade-off.
- Prefer clear names over comments.
int daysUntilExpiryneeds no comment;int d; // days until expirydoes. - Document public APIs with Javadoc: purpose, parameters, return value, exceptions and edge cases.
- Keep comments up to date. A wrong comment is worse than none.
- Do not leave dead code commented out; version control remembers old code for you.
- Use
TODOandFIXMEmarkers sparingly and with context; IDEs list them for you.
// Bad: repeats the code
i = i + 1; // add one to i
// Good: explains the reason
attempts++; // the API counts the first call as attempt 1, so start reporting from 1
10. Do Comments Affect Performance?
No. The compiler discards comments, so they never reach the .class file and have no effect on speed or size at runtime. Javadoc comments are only read by the javadoc tool and IDEs.
11. Summary
- Java has single-line (
//), multi-line (/* */) and documentation (/** */) comments. - Block comments cannot be nested.
- Javadoc comments with tags such as
@paramand@returngenerate HTML API documentation. - Comments do not affect the compiled program.
- Good comments explain intent and stay accurate; clear code needs fewer comments.
Related Tutorials
12. Official Java Documentation References
- Java Language Specification (Java SE 25) §3.7: Comments
- JLS §3.3: Unicode Escapes
- Documentation Comment Specification for the Standard Doclet (Java SE 25)
- The javadoc Command (Java SE 25)
- Javadoc Guide (Java SE 25)
- JEP 467: Markdown Documentation Comments
- JEP 413: Code Snippets in Java API Documentation