comments-in-java

Comments in Java

In a program, remarks partake in causing the program to turn out to be more comprehensible by setting the detail of code included and appropriate utilization of remarks makes support simpler and discovering bugs without any problem. Remarks are disregarded by the compiler while gathering a code.

In Java there are three kinds of remarks:

  1. Single – line remarks.
  2. Multi – line remarks.
  3. Documentation remarks.

Single-line Comments

An apprentice level developer utilizes generally single-line remarks for depicting the code usefulness. Its the most simplest composed remarks.

Linguistic structure:

//Comments here( Text in this line only is considered as comment )

Example:

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

Multi-line Comments

To depict a full technique in a code or a perplexing piece single line remarks can be dull to compose, since we need to give ‘//’ at each line. So to beat this multi line remarks can be utilized.

Linguistic structure:

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

Example:

//Java program to show 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*/
    }
}

We can also accomplish single line comments by using the above syntax as shown below:

/*Comment line 1*/

Documentation Comments

This sort of remarks are utilized for the most part when composing code for a task/programming bundle, since it assists with creating a documentation page for reference, which can be utilized for getting information about techniques present, its boundaries, and so forth

For example http://docs.oracle.com/javase/7/docs/programming interface/java/util/Scanner.html is an auto created documentation page which is produced by utilizing documentation remarks and a javadoc instrument for preparing the remarks.

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

Available tags to use:

TAGDESCRIPTIONSYNTAX
@authorAdds the author of a class.@author name-text
{@code}Displays text in code font without interpreting the text as HTML markup or nested javadoc tags.{@code text}
{@docRoot}Represents the relative path to the generated document’s root directory from any generated page.{@docRoot}
@deprecatedAdds a comment indicating that this API should no longer be used.@deprecated deprecatedtext
@exceptionAdds a Throws subheading to the generated documentation, with the classname and description text.@exception class-name description
{@inheritDoc}Inherits a comment from the nearest inheritable class or implementable interface.Inherits a comment from the immediate surperclass.
{@link}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
@returnAdds a “Returns” section with the description text.@return description
@seeAdds a “See Also” heading with a link or text entry that points to reference.@see reference
@serialUsed in the doc comment for a default serializable field.@serial field-description | include | exclude
@serialDataDocuments the data written by the writeObject( ) or writeExternal( ) methods.@serialData data-description
@serialFieldDocuments an ObjectStreamField component.@serialField field-name field-type field-description
@sinceAdds a “Since” heading with the specified since-text to the generated documentation.@since release
@throwsThe @throws and @exception tags are synonyms.@throws class-name description
{@value}When {@value} is used in the doc comment of a static field, it displays the value of that constant.{@value package.class#field}
@versionAdds a “Version” subheading with the specified version-text to the generated docs when the -version option is used.@version version-text
//Java program to illustrate frequently used 
// Comment tags
 
/**
* <h1>Find average of three numbers!</h1>
* The FindAvg program implements an application that
* simply calculates average of three integers and Prints
* the output on the screen.
*
* @author  Pratik Agarwal
* @version 1.0
* @since   2017-02-18
*/
public class FindAvg 
{
    /**
    * This method is used to find average of three integers.
    * @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;
    }
 
    /**
    * This is the main method which makes use of findAvg method.
    * @param args Unused.
    * @return Nothing.
    */
 
    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);
    }
}

Output:

Average of 10, 20 and 30 is :20

For the above code documentation can be created by utilizing a device ‘javadoc’ :

Javadoc can be utilized by running the accompanying order in terminal.

javadoc FindAvg.java

This article is contributed by Pratik Agarwal. On the off chance that you like GeeksforGeeks and might want to contribute, you can likewise compose an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article showing up on the GeeksforGeeks principle page and help different Geeks.

If you don’t mind compose remarks on the off chance that you discover anything mistaken, or you need to share more information about the theme examined previously.

Consideration peruser! Try not to quit adapting now. Get hold of the relative multitude of important Java Foundation and Collections ideas with the Fundamentals of Java and Java Collections Course at an understudy agreeable cost and become industry prepared.