Java Comments

Comments are used to explain logic or disable code. Comments do not affect code and are excluded from compilation, or program interpretation.

There are three types of comments that can be used in Java.

Single-Line Comment

Everything from // to the end of that line is treated as a comment.

public static void main(String[] args) {
    // When declaring an integer variable, write code as follows.
    int num1;
}

Multi-Line Comment

Everything from /* to */ is treated as a comment.

public static void main(String[] args) {
    int num1 = 100;
    int num2 = 200;
   
    /* The code below calculates and displays the sum of the declared integers.
       num1 is 100 and num3 is 200, so the result is 300. */
    System.out.println(num1 + num2);
}

Because multi-line comments cannot be nested by their nature, be careful. For this reason, it is generally recommended to use // first.

Document Comment

Everything from /** to */ is treated as a comment. It is used in HTML files generated by javadoc to document a program. HTML tags can be used in comment content.

/**
 * The main method is the starting point of execution for a Java application.
 */
public static void main(String[] args) {
    String str = null;
}