Comments in Java

Comments

The computer does not execute comments. They are only used to explain the code.

Types Of Comments

  • Single Line Comment: For commenting single line of code. E.g.,// This is a single line comment.
  • Multi Line Comment: For commenting on multiple lines of code. E.g.,/* This is a multi line comment. */
Info

Note: You can write comments anywhere in your java program.

Single Line Comment Example

public class Main {
    public static void main(String[] args) {
        // This prints Hello World on screen
            System.out.println("Hello World");
    }
}
Show Output

Here // is a single line comment. This prints Hello World on screen is completely ignored by Java compilers.

Multi Line Comment Example

public class Main {
    public static void main(String[] args) {
             /* This is a multi line comment.
            You can also write here.
            */
            System.out.println("Hello World");
    }
}

Show Output
Here any text inside /*...*/ is multi line comment and completely ignored by Java compilers.

Why Comments Are Important?

Comments are important because they help you to understand your code. It also helps other programmers to understand your code. It is a good practice to write comments in your code.

Info

Note: Select the code and press ctrl+/ to comment on the code. To uncomment the code, select code and press ctrl+/. This will saves your time.