3 Types of Comments in Java – Why comments are Important?
Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
Java Comments are used in programs to make the code more understandable. Therefore, comments in Java (remarks) make a program more intelligible as they set out the details of the code. However, appropriate utilization of remarks also makes support simpler and helps discover bugs effectively. The compiler disregards the comment while aggregating code, i.e., the compiler won’t read them because these statements are non-executable.
However, we are going to discuss different types of Java comments with their syntax and examples. Last but not least, we will cover the complete table of comments used in Java.
Let’s take the driving seat and speed up your programming skills.
To master the concept of comments, you should know the Basic Java Syntax.
Types of Comments in Java
There are three types of Java comments:
Single-line Comments in Java
These are used when the comment written is short or a one-liner. Therefore, the single-line comment in Java is denoted by using two forward slashes before a statement.
Syntax of single-line comments in Java
// A comment is written here
Example of Java single-line comment:
class Scomment
{
public static void main(String args[])
{
// Single line comment here
System.out.println("Single line comment above");
}
}Multi-line Comments in Java
Whenever we need to explain a procedure, we use Java multiline comments. Whereas single-line comments become tedious in this case, as we will need to write ‘//’ at the start of every line, therefore we use multiline comments in Java.
Syntax of multi-line comment in Java-
/*Comment starts continues continues . . . Comment ends*/
Let’s take a break and learn Classes and Objects in Java.
Example of multi-line comment in Java:
class Scomment
{
public static void main(String args[])
{
System.out.println("Multi line comments below");
/*Comment line 1
Comment line 2
Comment line 3*/
}
}Documentation Comments in Java
Although this kind of Java comment 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, their parameters, and so forth.
Syntax of Java documentation comments –
/**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 of documentation comment in Java –
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();
}
}
}Recommended Reading – Primitive & Non-Primitive Data types with Examples
Output-
Benefits of Using Comments in Java
However, there are several advantages of using comments in your Java code:
1. Enhanced Code Readability: Comments act as explanatory notes, making your code easier to understand for yourself and others. Therefore, well-placed comments can clarify complex logic, the purpose of variables and methods, and the overall program flow. However, this is especially beneficial when revisiting your code after some time or collaborating with other programmers.
2. Improved Maintainability: Comments can significantly simplify code maintenance. Wherever Codebases evolve, modifications become more manageable when the code is well-commented. Hence, Comments explain the reasoning behind specific code sections, making it easier for developers to understand the intent and adjust the code as needed.
3. Efficient Debugging: Also, comments can aid in debugging by providing clues about the expected behavior of different code sections. However, when an error arises, tracing the code’s execution with the help of comments can streamline the troubleshooting process. Therefore, comments can also indicate potential issues or areas for improvement.
4. Future Updation: Adding a comment to your code simplifies making the changes. Therefore, it helps to switch to the particular part of the program rather than understanding the whole code. Hence, For future use it becomes easier for other programmers to understand the program and make updates.
Table of Java Comments
| Tag | Description | Syntax |
| @serialField | Used to document an ObjectStreamField component | @serialField field-name field-type field-description |
| @since | Adds a “Since” heading to the generated document. | @since release |
| @throws | Synonym to @since | @throws class-name description |
| {@value} | When {@value} is used in the comment of the document of a static field, it displays the value of that constant. | {@value package.class#field} |
| @version | This method adds a “Version” subheading along with the specified version-text to the generated docs when the -version option is used. | @version version-text |
| {@link} | 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. | {@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 | Adds a parameter with the specified parameter-name followed by the specified description to the “Parameters” section. | @param parameter-name description |
| @return | This method adds a “Returns” section with the description text. | @return description |
| @see | This method adds a “See Also” heading with a link or text entry that points to reference. | @see reference |
| @serial | This method is used in the document comment for a default serializable field. | @serial field-description | include | exclude |
| @serialData | This method documents the data written by the writeObject( ) or writeExternal( ) methods. | @serialData data-description |
| @author | It is used to add the author of a class. | @author name-text |
| {@code} | It displays text in code font without interpreting the text as HTML markup or nested javadoc tags. | {@code text} |
| {@docRoot} | This method is used to represent the path relative to the generated root directory page | {@docRoot} |
| @deprecated | This method adds a comment indicating that this API should be discontinued | @deprecated deprecatedtext |
| @exception | It adds a Throws subheading to the generated documentation, with the classname and description text. | @exception class-name description |
| {@inheritDoc} | Used to inherit the comment from the implementable interface or the nearest inheritable class. | Inherits a comment from the immediate surperclass. |
Summary
As a result, Comments in Java are used to provide some extra information about the code. Single-line, multi-line, and documentation are the three ways to present the comments in Java. Although these are optional, the programmer is not bound to use them. Hence, the remarks are only for providing a better understanding of the code.
Now, it’s the right time to discuss Variables in Java
Hope you like the explanation. Please share your experience in our comment section.
Did we exceed your expectations?
If Yes, share your valuable feedback on Google



So good
Thank you for Complimenting our “Java Comments” Tutorial. Keep Exploring Java Programming Tutorials from Data-Flair.
Thank you for sharing this, it helped me..
Shrija, all this stuff is just for your help. It seems that you have a great interest in learning Java. For making your learning easy and faster we have designed a special page for you where you can get all the material related to Java and other technology as well. You must visit Java Tutorials Home
Shrija, all this stuff is just for your help. It seems that you have a great interest in learning Java. For making your learning easy and faster we have designed a special page for you where you can get all the material related to Java and other technology as well. You must visit Java Tutorials Home
very informative and helpful. thanks for sharing
Informative
It is crucial and helpful to me
God bless you
Thanks
its important and helpful for me.
Excellent Article on java it was helpful
Thanks & Regards
V.saibabu
Thank You for sharing this informative blog. Everyone got a lot of help from this blog. So, keep sharing this type of descriptive and informative information. If anybody want to Write an article on Technology Visit Write For Us Technology on I love Aussie.
Comment the line
This comment
This article offers a detailed and clear explanation of Java comments, their types, and benefits, making it an excellent resource for both beginners and advanced programmers. A great read to enhance code readability and maintainability!