The Java comments are the statements that are not executed by the compiler and interpreter. The comments can be used to provide information or explanation about the variable, method, class, or any statement. It can also be used to hide program code.
There are three types of comments in Kava.
Single Line Comment
Multi-Line Comment
Documentation Comment
Single Line Comment
The single-line comment is used to comment only one line.
Syntax:
// this is a comment
Example:
public class ComputeArea {
public static void main(String[] args) {
radius = 20; // the radius
PI = 3.14; // the PI value
area = radius * radius * PI; // the area formula
System.out.println(area);// output the area
}
}
2. Multi-line Comment
The multi -ine comment is used to comment on multiple lines of code.
Syntax:
/* This is also a comment */
Example:
/* This program will calculate the area of a circle.
Let say the radius is 20. PI is a constant (3.14).
And the formula for area is radius*radius*PI.
*/
public class ComputeArea {
public static void main(String[] args) {
radius = 20;
PI = 3.14;
area = radius * radius * PI;
System.out.println(area);
}
}
3. Document Comment
The documentation comment is used to create documentation API. To create a documentation API, you need to use javadoc tool.
Syntax:
/** This is also a comment */
Example:
/** The Calculator class provides methods to get addition and subtraction of given 2 numbers.*/
public class Calculator {
/** The add() method returns addition of given numbers.*/
public static int add(int a, int b){return a+b;}
/** The sub() method returns subtraction of given numbers.*/
public static int sub(int a, int b){return a-b;}
}