3 Types of Comments in Java – Why are They So Important?

Java Comments are used in programs to make the code more understandable. Comments in Java (remarks) make a program more intelligible as they set the details of the code. Appropriate utilization of remarks also makes support simpler and discovering bugs effectively. They are disregarded by the compiler while aggregating a code, i.e. the compiler won’t read them because these statements are non-executable.

In this article, we are going to discuss different types of Java comments with their syntax and example. Last but not least, we will cover the complete table of comments used in Java.

So, take the driving seat and speed up your programming skills.

To master the concept of comments, you should know the Basic Java Syntax.

Types of Comments in Java

There are three types of Java comments:

Types of Comments in Java

1. Single-line Comments

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

Syntax-

// A comment is written here

Example-

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

2. Multi-line Comments

Multi-line 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.

Syntax-

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

Let’s take a break and learn Classes and Objects in Java.

Example –

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

3. Documentation Comments

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

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

Example 

package JavaCommentsDemo;
//Program to illustrate comments in Java
/**
* <h1>Find sum of two numbers!</h1>
* FindSum program finds the sum
*and gives the output on
*the screen.
*
* @author  dataflair
*/
public class FindSum 
{
	/**
	    * Method to find average
	    * @param numA- This is the first parameter to calculateSum method
	    * @param numB - This is the second parameter to calculateSum method
	    */
	int numA;
	int numB;
FindSum(int numA,int numB)
{
	this.numA=numA;
	this.numB=numB;
}
void calculateSum()
{
	System.out.println("Sum of two numbers is "+(numA+numB));
}
static class Test
{
	public static void main(String args[])
	{
		FindSum obj=new FindSum(10,20);
		obj.calculateSum();
	}
}
}

Recommended Reading – Primitive & Non-Primitive Data types with Examples

Output-

FindSum

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 to 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}Used to inherit the comment from the implementable interface or nearest inheritable class.Inherits a comment from the immediate surperclass.

Summary

Comments in Java are used to provide some extra information about the code. Single line, multi-line and documentation are the three ways to present the comments in Java. These are optional; a programmer is not bounded to use them. Remarks are only for providing a better understanding of the code.

Now, it’s the right time to discuss Variables in Java 

Hope, you like the explanation. Please share your experience in our comment section.

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 *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.