Java Comments
The Java comments are the explanatory statements which will not get executed by the interpreter and the compiler. It allows you to mention the working of any statement or used to hide the program code.
There are three types of comments that are supported by Java-
- Single line comment
- Multiple line comment
- Documentation comment
Single line Java comment
If you want to comment the single line then you can use the single line java comment using “//” at the start of the statement.
Example-
public class Example1 {
public static void main(String[] args) {
int i=5;//Here, i is a variable
System.out.println(i);
}
}
Output-
5
Multi-line java comment
If you want to comment multiple lines then you can use multi-line java comment. The statements can be commented between “/*....*/”.
Example-
public class Example2 {
public static void main(String[] args) {
/* Let's declare and
print variable in java. */
int i=5;
System.out.println(i);
}
}
Output-
5
Documentation comment
You can use this comment when you are creating the documentation API. For this, you have to use the Javadoc tool.
Syntax-
/**
documentation
comment
*/
Example-
public class Calculator {
/** The add() method.*/
public static int add(int a, int b){return a+b;}
/** The sub() method */
public static int sub(int a, int b){return a-b;}
}
Above code can be compiled by javac tool and then create the documentation API using the Javadoc tool.
javac Calculator.java
javadoc Calculator.java
This will create the HTML file for the calculator class in the directory.