Comments in Java | Types, Example

Comments in Java are statements that describe the features of a program. It allows the programmers to compose and express their thoughts related to the code independently.

It is a good habit to write comments related to code in a program.

Java comments are not executed by the compiler and interpreter. They can be used to provide information about variables, methods, classes, or any statement.

For example, here is a comment that explains the value used to initialize a variable “x”:

double x = 50.50; // Variable declaration and initialization.

This comment explains the declaration and initialization of a variable with a value. Comments can also be used to hide program code.

Why should we write Comments in Java Program?

We should write comments related to the code because comments help programmers who read your code understand your thoughts.

This means that comments increase the readability and understandability of the program. So, if a program is understandable, then it helps others to easily follow our code.

If other members of the application development team do not understand our program, then they may not be able to implement it in the project and will reject your code.

Therefore, writing comments is compulsory in any program to make the code understandable. In addition, we will find comments helpful when we review our own program code.

Types of Comments in Java

There are three types of comments in Java that are as follows:

  • Single line comment
  • Multi line comment
  • Java documentation comment

Let us understand all of them one by one.

Single line comment:

It is used for making a single line as a comment. Single line comment is also called inline comment that provides brief summary comments for a single line.

It begins with a double slash symbol / / and after this, whatever is written till the end of the line is considered as a comment.

For example:

// Variable declaration for Compund Interest.
     float pincipalAmount, timePeriod, rateOfInterest;

We can also use the trailing comment to provide an explanation for a single line of code. Start trailing comments with a double slash (//) and place it to the right of the line of code it refers to.

For example:

int s = a + b + c; // Adding three numbers and store it into a variable s of type int.

Multi line comments:

These comments are used for commenting several lines as comments. Multi line comments begins with / * and end with * /. In between / * and * /, whatever will be written, will be treated as a comment by Java and everything will ignore by java compiler until it finds a */.

For example:

multi line  

Java documentation comments:

These comments begins with / * * and end with * /. Java documentation comments are used to provide explanations for every feature in a Java program.

This explanation proves helpful in the development of a .html file called API (Application Programming Interface) document.

For example:


Hope that this tutorial has covered all the important points related to Java comments with examples. I hope that you will have understood the basic points.
Thanks for reading!!!
Next ⇒ Escape Sequence in Java

⇐ Prev Next ⇒