Java Comments represent the ail and description of a program.While writing any code in any technology developer will mention comments to understand the coding lines, compiler will not execute those comments and memory will not be allocated to an application. The main advantage of Java Comments are readability. Comments can be defined as three ways in Java as follows.
Single line comment starts with // .
Multiline comments starts with /* ends with */ .
Java documentation comments starts with /** ends with */ .
Example
Following is an example which describes what is the purpose of Java Comments.
leapyear.java
[java]//simple program to check whether year is leap year or not
import java.util.Scanner;
public class leapyear {
private static Scanner input;
/**A leap year in the Gregorian calendar has an extra day for February.
*A leap year has 366 days.
*
* Algorithm to find a leap year
* -------------------------------
* if year % 400 = 0, leap year
* else if year % 100 = 0, not a leap year
* else if year % 4 = 0, leap year
* else, not a leap year
*/
public static void main(String args[])
{
int year;
System.out.println("Enter the year.");
Scanner s=new Scanner(System.in);
year = s.nextInt();
if(year%400==0 || year%100 ==0 || year%4==0){
System.out.println("Entered number is leap year.");
}
else{
System.out.println("it is not a leap year.");
}
}
}
[/java]
When compile the code following is the output will be displayed.
[java]
Enter the year.
2016
Entered number is leap year.[/java]
The following is an example for the single line comment.
[java] public class Sample {
public static void main(String[] args) {
int a=100;//Here, a is a variable
System.out.println(a);
}
} [/java]
Output: It produces the following result.
[java]100[/java]
The following is an example for the multiline comment.
[java]
public class Sample2{
public static void main(String[] args) {
/* Let's declare and
print variable in java. */
int a=100;
System.out.println(a);
}
} [/java]
Output: It produces the following result.
[java]100[/java]
Summary
Key Points
Java Comments - The comments will not be compiled by JVM.
Java Comments - The purpose of comment is to describe the code.