Java Comments
Comments are notes for humans. The compiler ignores them completely, so they never change what your program does. Good comments explain why code exists or point out something surprising; they shouldn't just repeat what the code already says.
Single-line comments
Two slashes // start a comment that runs to the end of the line. It can stand on its own line or follow code:
class Main {
public static void main(String[] args) {
// Greet the user before anything else
System.out.println("Welcome!");
System.out.println("Ready."); // shown after the welcome message
}
}Welcome!
Ready.
Multi-line comments
Text between /* and */ is a comment, even across many lines:
class Main {
public static void main(String[] args) {
/*
The shop is closed on Sundays,
so we only list six opening days.
*/
System.out.println("Open Monday to Saturday");
int hours = /* per day */ 8;
System.out.println("Hours per week: " + hours * 6);
}
}Open Monday to Saturday
Hours per week: 48
Note: Multi-line comments cannot be nested. A
*/inside the comment ends it early.
Commenting out code
A quick way to disable a line while experimenting is to turn it into a comment. The statement is still in the file, but it no longer runs:
class Main {
public static void main(String[] args) {
System.out.println("This line runs");
// System.out.println("This line is switched off");
System.out.println("This one runs too");
}
}This line runs
This one runs too
Most editors toggle comments with Ctrl+/ (or Cmd+/ on a Mac).
Javadoc comments
Java has a third kind of comment: /** ... */. These documentation comments are placed right above classes and methods. The javadoc tool turns them into HTML reference pages — the official Java API documentation is generated this way — and IDEs show them as pop-up help.
/**
* Calculates the area of a rectangle.
*
* @param width the width in centimetres
* @param height the height in centimetres
* @return the area in square centimetres
*/
static int area(int width, int height) {
return width * height;
}Common Javadoc tags:
| Tag | Describes |
|---|---|
@param name |
a method parameter |
@return |
the returned value |
@throws Type |
an exception the method may throw |
@see |
a link to related code |
@author, @since |
who wrote it and in which version it appeared |
Writing useful comments
| Less helpful | More helpful |
|---|---|
i = i + 1; // add one to i |
retries++; // the API sometimes fails once on cold start |
// loop |
// Process newest orders first so refunds are applied before shipping |
Prefer clear names over explanations: a variable called daysUntilExpiry needs no comment, while d does.
Test yourself
Which syntax starts a single-line comment in Java?
- #
- //
- --
- <!--
What is special about comments written as /** ... */?
- They are executed
- They are Javadoc comments used to generate documentation
- They are compiler errors
- They can only contain code
How do comments affect the compiled program?
- They slow it down
- They are printed at runtime
- They are ignored by the compiler
- They must be removed before compiling