TExcelTAD
Home › Java › Java Comments

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:

java · try it yourself
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:

java · try it yourself
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:

java · try it yourself
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.

java
/**
 * 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?

  1. #
  2. //
  3. --
  4. <!--

What is special about comments written as /** ... */?

  1. They are executed
  2. They are Javadoc comments used to generate documentation
  3. They are compiler errors
  4. They can only contain code

How do comments affect the compiled program?

  1. They slow it down
  2. They are printed at runtime
  3. They are ignored by the compiler
  4. They must be removed before compiling