Java Comments | Three Types of Comments in Java

1. Objective

In this tutorial, we are going to learn about Java comments. We will learn about the different types of comments in Java programming language: Single-line Comments, Multi-line Comments and Documentation Comments in Java with syntax and examples Programs. At last, we saw a table a table of Java Comments.

So, let us start with Java Comments.

Java Comments | Three Types of Comments in Java

Java Comments | Three Types of Comments in Java

2. Java Comments

Java Comments are used in programs to make them more understandable, in a program. Comments in Java are participating in influencing the program to end up more intelligible by setting the detail of code included and appropriate utilization of remarks makes support simpler and discovering bugs effectively. Remarks are disregarded by the compiler while aggregating a code, i.e. they compiler won’t read them.

Do you know about Wrapper Class in Java – Autoboxing & Unboxing With Examples

There are three types of Java comments:

  • Single – line remarks
  • Multi – line remarks
  • Documentation remarks

3. Types of Comments in Java

There are 3 types of Comments in Java

a. Single-line Comments in Java

As the name suggests, it is for the beginners and is in a single line Java comments.

A Syntax of Single-line Comments–

// A comment is written here

Example of Single-line Comments–

class Scomment
{
    public static void main(String args[])
    {
         // Single line comment here
         System.out.println("Single line comment above");
    }
}

Read about Hierarchical Data Structure in Java 

b. Multi-line Comments in Java

Multiline Java comments are used wherever we need to explain a procedure, single line comments become tedious in this case as we will need to write ‘//’ at the start of every line.
A Syntax of Multi-line Comments –

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

Example of Multi-line Comments –

class Scomment
{
    public static void main(String args[])
    {
        System.out.println("Multi line comments below");
        /*Comment line 1
          Comment line 2
          Comment line 3*/
    }
}

Let’s explore Java Generics in detail

c. Documentation Comments in Java

This kind of Java comments is utilized by and large when composing code for an undertaking/programming bundle, since it produces a documentation page for reference, which can be utilized for getting data about strategies show, its parameters, and so forth.

A Syntax of Documentation Comments–

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

Example of Documentation Comments 

//Program to illustrate comments in Java
/**
* <h1>Find average of three numbers!</h1>
* FindAvg program finds the average
*and gives the output on
*the screen.
*
* @author  dataflair
*/
public class FindAvg
{
    /**
    * Method to find average
    * @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 findAvg(int numA, int numB, int numC)
    {
        return (numA + numB + numC)/3;   
 }
    public static void main(String args[])
    {
        FindAvg obj = new FindAvg();
        int avg = obj.findAvg(10, 20, 30);
        System.out.println("Average of 10, 20 and 30 is :" + avg);
    }
}

Let’s know about Java Garbage Collection Algorithm in detail

4. Table of Java Comments

TAGDESCRIPTIONSYNTAX
@serialFieldUsed to document an ObjectStreamField component@serialField field-name field-type field-description
@sinceAdds a “Since” heading to the generated document.@since release
@throwsSynonym to @since@throws class-name description
{@value}When {@value} is used in the comment of the document of a static field, it displays the value of that constant.{@value package.class#field}
@versionThis method adds a “Version” subheading along with the specified version-text to the generated docs when the -version option is used.@version version-text
{@link}This method inserts an in-line link with the visible text label that points to the documentation for the specified package, class, or member name of a referenced class.{@link package.class#member label}
{@linkplain}Identical to {@link}, except the link’s label is displayed in plain text than code font.{@linkplain package.class#member label}
@paramAdds a parameter with the specified parameter-name followed by the specified description to the “Parameters” section.@param parameter-name description
@returnThis method adds a “Returns” section with the description text.@return description
@seeThis method adds a “See Also” heading with a link or text entry that points to reference.@see reference
@serialThis method is used in the document comment for a default serializable field.@serial field-description | include | exclude
@serialDataThis method documents the data written by the writeObject( ) or writeExternal( ) methods.@serialData data-description
@authorIt is used to add the author of a class.@author name-text
{@code}It displays text in code font without interpreting the text as HTML markup or nested javadoc tags.{@code text}
{@docRoot}This method is used represent the path relative to the generated root directory page{@docRoot}
@deprecatedThis method adds a comment indicating that this API should be discontinued@deprecated deprecatedtext
@exceptionIt adds a Throws subheading to the generated documentation, with the classname and description text.@exception class-name description
{@inheritDoc}This method is used to inherit the comment from the implementable interface or nearest inheritable class.Inherits a comment from the immediate surperclass.
This was all about Java Comments Tutorial. Hope you like our explanation on Types of Comments in Java.

5. Conclusion

Hence, in this Java tutorial, we learned about Java comments and different types of comments in Java: Single-line Comments, Multi-line Comments and Documentation Comments in Java with syntax and examples Programs. At last, we saw a table of Java Comments and also learn the use of Comments in Java. Furthermore, if you have any query feel free to ask in the comment section.

Related Topic- Type Conversion in Java

For reference

4 Responses

  1. Jothika says:

    So good

    • Data Flair says:

      Thank you for Complimenting our “Java Comments” Tutorial. Keep Exploring Java Programming Tutorials from Data-Flair.

  2. shrija says:

    Thank you for sharing this, it helped me..

    • DataFlair Team says:

      Shrija, all this stuff is just for your help. It seems that you have a great interest in learning Java. For making your learning easy and faster we have designed a special page for you where you can get all the material related to Java and other technology as well. You must visit Java Tutorials Home

Leave a Reply

Your email address will not be published. Required fields are marked *