C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Java programming language

Different types of comments in Java programming

In this java tutorial, we are going to learn what the comments are and how many types of comments are supported by java?
Submitted by IncludeHelp, on November 27, 2017

In Java, 'Comments' are given for those statements which are not executed by compiler and interpreter at run time. We can use comments to display the information about variables, class, methods and about any operation/logic.

It is also used to hide particular line, statement or any code in the program (which you do not want to execute and want to keep for future reference).

Types of comments in java

There are three types of comments in Java,

  1. Single line comment
  2. Multiline comment
  3. Documentation comment

1. Single line Comment (//)

It is represented by double slash (//) and used to comment only single line, the text which is written after these characters (//) will not be executed by the compiler.

Syntax:

// this line will not be executed 

2. Multi line Comments (/*...*/)

It is used to comment multiple lines in the program, because sometimes we need to explain/write more lines than a single line. So, for that purpose we need to use multi line Comment. These are represented by /* and */

Syntax:

/*
Below code is used to create an employee class, this code is written by me 
on 27 Nov 2017. The code has been tested successfully.
Etc.
*/

3. Documentation Comment (/**...*/)

In documentation comment, we can use documentation of our source code, basically it is used to describe particular code, function description etc.

When we use documentation comment, this become a part of 'Javadoc' , and Javadoc tool will be used for that.

Structure for Javadoc comment

/**
    Paragraph description
    Tag labels
    @param  ...
    @return ...
    @author ...
    @throws ...
*/

An example with documentation comment

/**
* This is a simple program to print Hello world!
*
* @author	Yamini Rawat
* @since	27/11/2017
* @reviewer	Alvin
*/
public class HelloWorld {

   public static void main(String[] args) {
      System.out.println("Hello World!");
   }
}

Output

Hello World!








COMMENTS