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.
The single line comment is used to comment only one line.
//This is single line comment
Example:
public class CommentExample1 { public static void main(String[] args) { int i=10;//Here, i is a variable System.out.println(i); } }
The multi line comment is used to comment multiple lines of code.
/* This is multi line comment */
public class CommentExample2 { public static void main(String[] args) { /* Let's declare and print variable in java. */ int i=10; System.out.println(i); } }
The documentation comment is used to create documentation API. To create documentation API, you need to use javadoc tool.
/** This is documentation comment */
/** 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;} }
Compile it by javac tool:
Create Documentation API by javadoc tool:
Now, there will be HTML files created for your Calculator class in the current directory. Open the HTML files and see the explanation of Calculator class provided through documentation comment.