Java Comments: Types with Examples

In this java tutorial, we are going to learn what the comments are and how many types of comments are supported by Java? By IncludeHelp Last updated : January 02, 2024

Java Comments

In Java, the 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 Java Comments

There are three types of comments in Java,

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

1. Java 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.

Example

// this line will not be executed

2. Java 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 */

Example

/*
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. Java 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 ...
*/

Example

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 and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.