Java Comments

Comments in Java are non-executable statements used to explain code, make it more readable, and help other developers understand what the code does. The compiler ignores comments, so they don't affect the program's execution.

Key Topics

1. Single-line Comments

Single-line comments start with //. Everything following // on that line is considered a comment and is ignored by the compiler.

// This is a single-line comment
System.out.println("Hello, World!"); // Prints Hello, World!

2. Multi-line Comments

Multi-line comments start with /* and end with */. Everything between these symbols is considered a comment.

/*
 * This is a multi-line comment.
 * It can span multiple lines.
 */
System.out.println("Multi-line comment example.");

3. Documentation Comments

Documentation comments start with /** and end with */. They are used by the Javadoc tool to generate HTML documentation for your code.

/**
 * The HelloWorld program implements an application that
 * simply displays "Hello, World!" to the standard output.
 *
 * @author Your Name
 * @version 1.0
 */
public class HelloWorld {
    /**
     * This is the main method which makes use of print statements.
     * @param args Unused.
     */
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Key Takeaways

  • Comments improve code readability and maintainability.
  • Use // for single-line comments.
  • Use /* ... */ for multi-line comments.
  • Use /** ... */ for documentation comments processed by Javadoc.