Java Comment

Hi everyone!! Welcome to flower brackets blog . In this post we are going to learn java comment.

In this java program we are going to display comments in java. This is going to be a brief explanation about comments in java.

Java Comment

What is java comment?

Comments in java are description added by the programmer to provide information or explanation within the program.

What is the use of comments in Java?

By adding (comments in java) if his colleague or another programmer looks at the program he can understand what exactly a variable, method, class or any statement is doing.

But sometimes it helps to leave yourself some notes in the code so that when you go back later and look at it you can understand what happened.

Also Read – Learn Operators In Java

Now these notes are called comments and they do not affect the actual program at all. If you add a comment or remove a comment it does not have any effect on the program that the compiler is going to make it.

It does not have any effect on how the program runs. They are simply there to help programmers understand what you have written.


What are the types of comments in Java?

That being said there are three types of comments in java they are,

  1. Single line comments
  2. Multi line comments
  3. Documentation

Single line comments

In java, comments which start with double slash is called single line java comment and it is used to comment (only single line) a single line.

Syntax:

// single line java comment example

Here’s an example on how do you write comments in java,

public class SingleLineComment
{
   public static void main(String[] args) 
   {
      int a = 5; // single line java comment example 
      System.out.println(a);
   }
}

Output:

5


Multi line comments

If you have multiple lines that you are leaving in comments you can use multiline java comment. It serves the same purpose as single line java comment.

You can start at the beginning of the text with a forward slash start ( / * ) and end with star forward slash ( */ ). It will comment out everything in between.

Compiler will not read this code it will completely ignore it.

Syntax:

/* 
This is 
an example for
multi line 
java comment 
*/

Example:

public class MultilineComment 
{
   public static void main(String[] args) 
   {
      /* Here we are declaring and 
      printing integer variable in java. */

      int b = 5; 
      System.out.println(b);
   }
}

Output:

5


Documentation Comments

Documentation comments look very similar to multiline comments but there is a difference. If you look at the beginning, instead of just having one star or asterisk it has two asterisks.

That’s the subtle difference between the two. Basically documentation comment is used to comment multiple lines of code but it also serves the specific purpose of writing documentation comments.

Syntax:

/** 
This 
is an example for 
documentation comment
java 
*/

Here’s an example:

/** 
This 
is an example for 
documentation comment
java. Here we are calculating area
of the square.
*/

public class DocumentationComment 
{
   public static void main(String[] args) 
   {
      double side = 5;      
      double area = side * side; 
      System.out.println("Area of Square is : " + area);
   }
}

Output:

Area of Square is : 25.0

In documentation comments we can use tags as well to specify parameter or method. Here are some available tags,

SyntaxTagDescription
@return description@returnadds a “Returns” section with the description text.
@exception class-name description@exceptionadds a Throws subheading to the generated documentation, with the classname and description text.
@author name-text@authoradds the author of a class.
{@docRoot}{@docRoot}represents the relative path to the generated document root directory from any generated page.
{@code text}{@code}displays text in code font without interpreting the text as HTML markup or nested javadoc tags.
@deprecated deprecatedtext@deprecatedadds a comment indicating that this API should no longer be used.
Inherits a comment from the immediate surperclass{@inheritDoc}inherits a comment from the nearest inheritable class or implementable interface.
@param parameter-name description@paramadds a parameter with the specified parameter-name followed by the specified description to the “Parameters” section.
{@linkplain package.class#member label}{@linkplain}identical to {@link}, except the link’s label is displayed in plain text than code font.
{@link package.class#member label}{@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.
@version version-text@versionadds a “Version” subheading with the specified version-text to the generated docs when the -version option is used.
{@value package.class#field}{@value}when {@value} is used in the doc comment of a static field, it displays the value of that constant.
@throws class-name description@throwsthe @throws and @exception tags are synonyms.
@since release@sinceadds a “Since” heading with the specified since-text to the generated documentation.
@serialField field-name field-type field-description@serialFielddocuments an ObjectStreamField component.
@serialData data-description@serialDatadocuments the data written by the writeObject( ) or writeExternal( ) methods.
@serial field-description | include | exclude@serialused in the doc comment for a default serializable field.
@see reference@seeadds a “See Also” heading with a link or text entry that points to reference.

Conclusion:

So that’s it for the post. I hope you have understood the concept to display java comment. Subscribe to flower brackets blog and share this post across social networks.

Related Posts