Home >>Java Tutorial >Java Comments

Java Comments

Java Comments

Java Comments are generally used in order to explain the Java code and enhance its readability. There are various uses of the Java comments as they can also be used while testing the alternative code just to prevent the execution of the code.

Types of Java Comments

There are generally two types of java comments that are as depicted below:

  • Single-line comments
  • Multi-line comments

1. Single- line comments

Single-line comments in Java generally starts with the two forward slashes (//) and any text that is between the // and at the end of the line then it is automatically ignored by the Java, in simple words; that content will not be executed.

Here is an example of the single-line comment example that will use the single-line comment just before a line of the code:

// This is a comment
System.out.println("Hello World");

Here is another example of the single-line comment example that will use the single-line comment at the end of the line of the code:

System.out.println("Hello World"); // This is a comment

2. Multi-line comments

Multi-line comments in Java generally starts with /* and it ends with */ and any text that is between the /* and */ then it is automatically ignored by the Java, in simple words; that content will not be executed.

Here is an example of the multi-line comment that will use the multi-line comment to elaborate the code:

/* The code below will print the words Hello World
to the screen, and it is amazing */
System.out.println("Hello World");

Which comment is to be used for better efficiency?

Well, this is a tricky question but the choice is yours that which one does you want to use. The basic use of the single line comments (//) is in short comments and the basic use of the multi-line comments (/* */) is for the longer comments in Java language.


No Sidebar ads