Java Comments

In this tutorial you will learn about the Java Comments and its application with practical example.

Java Comments

Comments are a set of statements that are not executed by the compiler. The use of comments makes it easy for humans to understand the source code. Usually comments gives you inside or explanation about the variable, method, class or any statement that exists in source code. The comment statements are ignored during the execution of the program.

Types of Java Comments

In Java, there are 3 types of comments.

  • Java Single Line Comment
  • Java Multi Line Comment
  • Java Documentation Comment

Java Single-line Comments

A ‘//’ (double forward slash) is used to specify a single line comment, which extends up to the newline character. This can be used to comments-out everything until a line break

Syntax:-

Example:-

Output:-

Java Multi-line Comments

If you want to comment multiple lines then you can do it using /* and */, everything in between from /* to */ is ignored by the compiler-

Uses:-

  • Useful for commenting out a section of code
  • Cannot be nested within other multi-line comments

Syntax:-

Example:-

Output:-

Java Documentation Comment

This is a special type of comment mainly used to generate documentation or reference for a project/software package. This can also be used to create documentation API. To create documentation API, we have to use javadoc tool.

Recommended:-  Java Decision Making Statements

Uses:-

  • Similar to multi-line comments but used to document Java code (classes, methods, fields)
  • Extracted using javadoc command-line utility

Available Tags:-

Tag Description Syntax
@author Used to specify 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} Used for relative path to the generated document’s root directory from any generated page. {@docRoot}
@deprecated Used to add a comment indicating that this API should no longer be used. @deprecated deprecatedtext
@exception Adds a Throws subheading to the generated documentation, with the classname and description text. @exception class-name description
{@inheritDoc} It inherits a comment from the nearest inheritable class or implementable interface. Inherits a comment from the immediate surperclass.
{@link} It is used to inserts an in-line link with the visible text label which links 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}
@param Used to specify a parameter with the specified parameter-name followed by the description. @param parameter-name description
@return Specify a “Returns” section with the description text. @return description
@see Specify a “See Also” heading with a link or text entry that points to reference. @see reference
@serial Used in the doc comment for a default serializable field. @serial field-description | include | exclude
@serialData Documents the data written by the writeObject( ) or writeExternal( ) methods. @serialData data-description
@serialField Documents an ObjectStreamField component. @serialField field-name field-type field-description
@since Used to specify a “Since” heading to the generated documentation. @since release
@throws The @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}
@version Used to specify version @version version-text
Recommended:-  Java Constants

Syntax:-

Example:-

Output:-

For the above program documentation can be generated by using a tool ‘javadoc’ as following –

Recommended:-  Java Installation

In this tutorial we have learn about the Java Comments and its application with practical example. I hope you will like this tutorial.