Java Comments

Java comments are the statements, we write to describe the code. We write comments for an only user perspective, hence compilers ignore Java comments at the time of compilation.

Types of Java Comments

There are three ways to write comments in Java.

  • Java Single Line Comment
  • Java Multi-Line Comment
  • Java Documentation Comment

Java Comments, Types of Java Comments with Example

Java Single Line Comment

It is the most used comment type, to describe the functionality of the code in a single line.
We can use a single line comment with ‘//’.

Syntax

//Comments here  (Compiler will ignore this line.)

//Java program to show single line comments 
class mysingleline 
{ 
  public static void main(String args[]) 
  {  
       // Single line comment here 
       System.out.println("Single line comment above");  
  } 
}
Example Output

Java Multi Line Comment

This type of comment is used to describe the code in more than one line. When we want to write comments more than one line we use ‘/* … */’ symbol.

Syntax

/*Comment starts
continues
continues
.
.
.
Commnent ends*/

//Java program to show multiline comments 
class Test 
{ 
  public static void main(String args[]) 
  {  
      System.out.println("Multi-line comments below"); 
      /*Comment line 1 
        Comment line 2  
        Comment line 3*/
  } 
}
Example Output

Java Documentation Comment and Javadoc tool

This type of comment is used generally when we are writing code for Software package. This type of comment helps in generating documents regarding Package. Whatever we write in comments will help to describe methods and variables and body as it will automatically include in Doc file.

For example, http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html is an auto-generated doc page which is generated by using Java doc type comments and a Javadoc tool for processing the comments. To write java Doc type comments we use ‘/** …….. */’ Symbol.

Syntax

/**Comment start
*
*tags are used in order to specify a parameter
*or method or heading
*HTML tags can also be used 
*such as <h1>
*
*comment ends*/

public class demo
{ 
    /** 
    * This method is used to find average of three integers. 
    * @param numA This is the first parameter to findAvg method 
    * @param numB  This is the second parameter to findAvg method 
    * @param numC  This is the second parameter to findAvg method 
    * @return int This returns average of numA, numB and numC. 
    */
    public int demo(int numA, int numB, int numC)  
    { 
        return (numA + numB + numC)/3; 
    } 
  
    /** 
    * This is the main method which makes use of findAvg method. 
    * @param args Unused. 
    * @return Nothing. 
    */
  
    public static void main(String args[])  
    { 
    	demo obj = new demo(); 
        int avg = obj.demo(10, 20, 30); 
  
        System.out.println("Average of 10, 20 and 30 is :" + avg); 
    } 
}
Example Output

To generate documentation for the following code we can use Javadoc command.
We can use the following command:

Syntax:

javadoc FindAverage.java

It will create an HTML file, we can open it in the browser and see the documentation description of our code.

Share