How to comment in Java?

Commands are very important in computer programming. These are simple explanations of the source code that make the source code more understandable to humans. However, these are not considered by the compiler or the interpreter.

Importance of comments

As discussed above, comments are necessary because they make a computer program more understandable. Pros of comments are listed below.

  • Makes the code easy to read.
  • Effortless code maintenance and error detection.
  • Provide details about a certain method, class, variable, or statement.
  • Functions written for use by others become easier to understand.

Like in other programming languages you can also write comments in Java. This write-up explores various types of java comments and how to use them along with their examples.

Types of Java Comments

In java, there are three approaches to comment as shown below.

1. Single Line Comment

In order to comment on a single line single line comments are used that begin with two forwards slashes. Text written after these forward slashes is ignored by the Java compiler.

Here is the syntax of the Java single-line comment:

// This is a single-line comment


2. Multi-Line Comment

When you want to comment multiple lines in your Java source code then use a multi-line comment. It begins with /* and ends with */. Text written in between these will not be executed by the Java compiler.


/* This is a multi-line comment */


3. Documentation Comment

Documentation comments are usually used in creating documentation API for larger java programs. These documentation APIs are used to reference classes, methods, and arguments used in the source code. It begins with /** and ends with */.

Here is the syntax of documentation type comment in Java.

*To depict parameters we use various tags
*or method or heading
*Or we can use HTML tags


Table given below covers multiple types of javadoc tags.

Tag Name Syntax Description
@author @author name-text It is used to write the author name of a particular class.
@version @version version-text It is used to mention version text.
@param @param-parameter name description It is used to add parameter name and description.
@return @return description It is used to find the return values easily by making a “Returns” section.
@deprecated @deprecated deprecated text It is used for indication of a deprecated class or method or filed and creates a warning every time it is used by someone.
@since @since release It is used to specify the version of method or class etc by adding the “since” section.
@throws @throws class-name description It is used to throw an exception.
@exception @exception class-name description It has a similar use as the @throw tag.
@see @see reference It is used to add a reference to a method or class by generating a link in the “see also” section.
@serial @serial field-description | include | exclude It is used to add relevant information about serialized fields.
@serialField @serial field-name field-type field-description It is used to document the ObjectStreamField component.
@serialData @serialData data-description It is used to document data written by methods such as writeObject( ) or writeExternal( ).
{@docRoot} {@docRoot} It is used to show root directory path.
@code {@code text} It is used for displaying text in code fonts.
{@value} {@value package.class#field} It is used to display the value of the constant when a doc comment is written in a static field.
{@inheritDoc} —– It is used to inherit a comment from an inheritable class.
{@link} {@link package.class#member label} It includes a link that focus the documentation for a particular package, class, or member name of a class that is referrenced.
{@linkplain} {@linkplain package.class#member label} Similar to link with the only difference that the label of the link is displayed in plain text rather than code text.


There are three kinds of comments in Java. The first is a single-line comment that begins with two forward slashes ‘//’, the second is a multi-line comment that begins with /* and ends with */, while the last is a documentation comment that is used to create documentation API for large Java programs and applications. All of these types of comments are explained in this tutorial along with javadoc tags that are used in documentation comments.

About the author

Naima Aftab

I am a software engineering professional with a profound interest in writing. I am pursuing technical writing as my full-time career and sharing my knowledge through my words.