Java Comments


In Java or other programming languages there are some lines that are not compiled and executed by the compiler and interpreter. These lines are called comments in any programming language. In programs we use comments to add more information to the program or to explain the program for example to describe the functionality of a module or function that is defined in our program. We can also add information about variables and classes or any other statements. We can also use comments to hide the program code for a specific time period.


Types of Java Comments:

In Java there are three types of comments:

  1. Single Line comment
  2. Multiline comment
  3. Documentation comment

All the types of comments in Java are described below:


Java single Line comment:

In java programming language the single line comments are used to add comments on only one line. The following is the syntax of single line comments in Java programming language.

Syntax:

The single line can be used in the following way:

//A single line comment in Java


Example of single line comment:

Consider the following example in which we have used a single line comment in a program to add more information about the program:

CODE:

public class ExampleComment {

public static void main (string [] args) {

int n = 4;

system. out. println (n); //printing the value of n

}

}

OUTPUT:

4

In the above example a variable is declared and is initialized at 4. The value of the variable is then printed and we added a comment in this line to indicate that what is going to be printed by the print statement.


Java Multi line comment:

In java programming language the multi line comments are used to add comments on multiple lines. The following is the syntax of multi line comments in Java programming language.

Syntax:

In Java programming language the multi line comments are in the following way.

/* Adding multi

line comments

to our program

*/


Example of multi line comments:

Consider the following example in which we have added multi line comments to our program to add more information that what our program is about:

CODE:

public class ExampleComment {

public static void main (string [] args) {

/*Declaring and

printing a variable

*/

int n = 4;

system. out. println (n);

}

}

OUTPUT:

4

In the above example we declared a variable and initialized it at 4. The value of the variable is then printed and we added a multi line comment before declaring the variable to indicate that we will be declaring a variable and will be printing its value in this program.


Java documentation comment:

In Java programming language the documentation comment is used to create the documentation API. We use the javadoc tool to create a documentation API.

The following is the syntax of the documentation comment in Java:

Syntax:

The documentation comment is used in java programming language in the following way:

/**

Adding a documentation

comment in

our program

*/


Example of documentation comment in Java:

Consider the following example in which we added the documentation comment to add information to our program:

CODE:

/** In this class we will be performing addition of two integers. */

public class addition {

/** using the add method to return the sum of two integers. */

public static void add (int a, int b) {

return a + b;

}

}

The above program will be compiled by using the following command:

javac addition. java

Now we will create the documentation API by using the javadoc tool. Use the following command to create the documentation API:

javadoc addition. java

In this way the html file will be created for the class that was named as addition in the current directory. When we open that html file then the explanation that was added by us as the documentation comment will be displayed of the class “addition”. Remember that this API was provided by the documentation comment.