March 21, 2020

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.

Types of Comments in Java

To get in-Depth knowledge on Java you can enroll for a live demo on Java Online Training

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

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.

Take your career to new heights of success with Java Training

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();
  }
}
}

Output

To learn more about Types of Comments in Java and other great features of Java, you can

enroll for a live demo on java online course

Java Comments

1. Tag: @serialField

Description: Used to document an ObjectStreamField component

Syntax: @serialField field-name field-type field-description

2. Tag: @since

Description: Adds a “Since” heading to the generated document.

Syntax: @since release

3. Tag: @throws

Description: Synonym to @since

Syntax: @throws class-name description

4. Tag: {@value}

Description: When {@value} is used in the comment of the document of a static field, it displays the value of that constant.

Syntax: {@value package.class#field}

5. Tag: @version

Description: This method adds a “Version” subheading along with the specified version-text to the generated docs when the -version option is used.

Syntax: @version version-text

6. Tag: {@link}

Description: 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.

Syntax: {@link package.class#member label}

7. Tag: {@linkplain}

Description: Identical to {@link}, except the link’s label is displayed in plain text than code font.

Syntax: {@linkplain package.class#member label}

8. Tag: @param

Description: Adds a parameter with the specified parameter-name followed by the specified description to the “Parameters” section.

Syntax: @param parameter-name description

9. Tag: @return

Description: This method adds a “Returns” section with the description text.

Syntax: @return description

10. Tag: @see

Description: This method adds a “See Also” heading with a link or text entry that points to reference.

Syntax: @see reference

11. Tag: @serial

Description: This method is used in the documentation comment for a default serializable field.

Syntax: @serial field-description | include | exclude

12. Tag: @serialData

Description: This method documents the data written by the writeObject( ) or writeExternal( ) methods.

Syntax: @serialData data-description

13. Tag: @author

Description: It is used to add the author of a class.

Syntax: @author name-text

14. Tag: {@code}

Description: It displays text in code font without interpreting the text as HTML markup or nested Javadoc tags.

Syntax: {@code text}

15. Tag: {@docRoot}

Description: This method is used to represent the path relative to the generated root directory page

Syntax: {@docRoot}

16. Tag: @deprecated

Description: This method adds a comment indicating that this API should be discontinued

Syntax: @deprecated deprecatedtext

17. Tag: @exception

Description: It adds a Throws subheading to the generated documentation, with the class name and description text.

Syntax:@exception class-name description

18. Tag: {@inheritDoc}

Description: Used to inherit the comment from the implementable interface or nearest inheritable class.

Syntax: Inherits a comment from the immediate superclass.

Summary