Core Java - SPLessons

Java Comments

Chapter 7

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java Comments

Java Comments

shape Description

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.


shape Example

Following is an example which describes what is the purpose of Java Comments.
leapyear.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.");
		}
	}

}

When compile the code following is the output will be displayed.

Enter the year.
2016
Entered number is leap year.

The following is an example for the single line comment.

    public class Sample {  
    public static void main(String[] args) {  
        int a=100;//Here, a is a variable  
        System.out.println(a);  
    }  
    }  

Output: It produces the following result.

100

The following is an example for the multiline comment.

   
 public class Sample2{  
    public static void main(String[] args) {  
    /* Let's declare and 
     print variable in java. */  
        int a=100;  
        System.out.println(a);  
    }  
    }  

Output: It produces the following result.

100

Summary

shape Key Points

  • Java Comments – The comments will not be compiled by JVM.
  • Java Comments – The purpose of comment is to describe the code.