Java Tutorial · Java Basics · Lesson 5 of 37

Java Comments

Single-line, multi-line and Javadoc comments.

Comment styles

  • // single-line comment.
  • /* ... */ multi-line comment.
  • /** ... */ Javadoc — documents classes and methods; the javadoc tool turns it into HTML documentation.
Example 1
public class Main {
    public static void main(String[] args) {
        // Print the area of a 3 x 4 rectangle
        System.out.println(area(3, 4)); // 12
        /* System.out.println("disabled"); */
    }

    /**
     * Returns the area of a rectangle.
     *
     * @param width  the width
     * @param height the height
     * @return width multiplied by height
     */
    static int area(int width, int height) {
        return width * height;
    }
}

Output

12

Run this on your computer — see “Get Started” for setup.

Test yourself

Which comment style is used to generate API documentation?

  1. //
  2. /* ... */
  3. /** ... */
  4. #
Show answer

Answer: /** ... */