Java ยท Chapter 6 of 42

Java Comments

Comments are notes ignored by the compiler, used to explain code. Java supports single-line comments with //, multi-line comments with /* */, and special Javadoc comments with /** */ used to generate documentation.

Syntax
// single line
/* multi
   line */
/** javadoc */

Single and multi-line comments

// comments out the rest of a line. /* ... */ can span multiple lines and is often used for longer explanations.

Javadoc comments

/** ... */ comments placed above classes or methods can be processed by the javadoc tool to auto-generate HTML documentation.

Example 1 (java)
public class Main {
  // prints a greeting
  public static void main(String[] args) {
    System.out.println("Hi!"); /* inline comment */
  }
}
Output
Hi!

Comments are ignored during compilation and do not affect output.

Key points

  • // starts a single-line comment.
  • /* */ wraps a multi-line comment.
  • /** */ is used for Javadoc documentation.
  • Comments do not affect program execution.
๐Ÿ’ก Note: Use Javadoc comments on public classes and methods in real projects for maintainability.

๐Ÿ“ Quick Quiz

1. Which starts a single-line comment?

2. Which comment style is used for Javadoc?

3. Do comments affect execution?