Java Comments
The Java comments are the statements in a program that are not
executed by the compiler and interpreter.
Why do we use comments in a code?
o Comments are used to make the program more readable by
adding the details of the code.
o It makes easy to maintain the code and to find the errors easily.
o The comments can be used to provide information or explanation
about the variable, method, class, or any statement.
o It can also be used to prevent the execution of program code
while testing the alternative code.
Types of Java Comments
There are three types of comments in Java.
1. Single Line Comment
2. Multi Line Comment
3. Documentation Comment
1) Java Single Line Comment
The single-line comment is used to comment only one line of the code.
It is the widely used and easiest way of commenting the statements.
Single line comments starts with two forward slashes (//). Any text in
front of // is not executed by Java.
Syntax:
1. //This is single line comment
Let's use single line comment in a Java program.
CommentExample1.java
1. public class CommentExample1 {
2. public static void main(String[] args) {
3. int i=10; // i is a variable with value 10
4. System.out.println(i); //printing the variable i
5. }
6. }