Java Comments

Java code can be made more understandable and explained via comments. The comments are not exceuted by compiler and interpreter. The comments will help a developer to understand their code when they refer the codes later.

There are 2 types of comments

  • Single-line Comments
  • Multi-line Comments

Single-line Comments

The single-line comment is denoted by two forward slashes(//). Any text between // and the end of the line is ignored by the compiler. Single-line comments are used to comment only a single line.

Example

public class Tutorial {
  public static void main(String[] args) {
    // Print the text to the terminal.
    System.out.println("My First Tutorial");
  }
}

Here, the following is the comments that is not executed by java.

// Print the text to the terminal.

Multi-line Comments

Comments with more than one line begin with /* and end with */. Java will ignore any text between the /* and */ characters.

Example

public class Tutorial {
  public static void main(String[] args) {
  
    /* The println function is used to print string to the screen.
    Here My First Tutorial will be printed to the screen */
    System.out.println("My First Tutorial");
  }
}

Most Read