Skip to content

Types of Comment in Java

In Java, comments are non-executable lines of text that programmers use to make the code more readable, document functionality, or temporarily disable certain parts of code during debugging. Java supports three types of comments:


1. Single-line Comments

  • Syntax: Begin with //
  • Use: Used for short explanations or notes. The comment applies only to the text following // on the same line.

Example:

public class SingleLineComment {

    public static void main(String[] args) {

        int x = 10; // This is a single-line comment

        System.out.println(x); // Printing the value of x

    }

}


2. Multi-line Comments

  • Syntax: Begin with /* and end with */
  • Use: Used for longer descriptions or notes spanning multiple lines.

Example:

public class MultiLineComment {

    public static void main(String[] args) {

        /*

         This is a multi-line comment.

         It can span multiple lines.

         */

        int x = 20;

        int y = 30;

        System.out.println(x + y); // Output: 50

    }

}


3. Documentation Comments (Javadoc Comments)

  • Syntax: Begin with /** and end with */
  • Use: These comments are used to document code and can be converted into HTML documentation using the javadoc tool. They are typically placed before classes, methods, or fields to explain their purpose and behavior.
  • Special Tags: Javadoc supports tags like @param, @return, and @throws to structure documentation.

Example:

/**

 * This class demonstrates Javadoc comments.

 */

public class JavadocComment {

    /**

     * This method adds two numbers.

     *

     * @param a the first number

     * @param b the second number

     * @return the sum of a and b

     */

    public int add(int a, int b) {

        return a + b;

    }

    public static void main(String[] args) {

        JavadocComment example = new JavadocComment();

        System.out.println(“Sum: ” + example.add(10, 20)); // Output: Sum: 30

    }

}

  • Generating Documentation: Run the javadoc command on your .java file to generate an HTML file with documentation.

javadoc JavadocComment.java


Key Points

  • Single-line Comments:
    • Simple and quick for brief notes.
    • Ideal for commenting specific lines.
  • Multi-line Comments:
    • Useful for detailed explanations or temporarily disabling blocks of code.
    • Avoid nesting multi-line comments as Java does not support it.
  • Javadoc Comments:
    • Essential for API documentation and professional codebases.
    • Helps in generating external documentation for projects.

Comment Best Practices

  1. Use Comments Wisely:
    1. Don’t overuse comments for obvious code. Write self-explanatory code with meaningful variable and method names.
    1. Example of redundant comment:

int x = 10; // Assign 10 to x (Avoid)

  • Keep Comments Updated:
    • Ensure comments are consistent with the code changes to avoid confusion.
  • Document Important Concepts:
    • Use comments to explain complex logic, assumptions, or design decisions.
  • Use Javadoc for Public APIs:
    • Properly document classes, methods, and fields exposed in public interfaces.