
Java Comments are description about the feature and working of a program. By using comments you describe the nature and flow of the program, functionality of the program.
Comments help others to follow our code easily and comments make the code more readable and understandable.
Comments are an integral part of any programming language like Java, C#, Python, C, C++. Comment helps the developer to read the code for better understanding.
There are three types of comments in Java:
- Single Line comments
- Multi line comments
- Documentation Comments
Table of Contents
Single line Comments in Java
When you need to mark a single line as a comment then you should go for Single line comment. These comments start with double slash symbol //. And whatever is written till the end of the line
// This is single line comment
Multiline Comments in Java
When you need to mark several lines as a comment then you should go for Multiline comment. These comments start with /* and ends with */.
/*
This is multi line comment
Statement 1
Statement 2
Statement 3
*/
Java documentation comments
Such comment starts with /** and ends with */. When you need to describe the every feature of the program then you should prefer the documentation comment. It is helpful to create a .html file API documentation.
You should use java documentation before every feature in the program.
/** Description about interface and class */
class Example {
}
/**
* Description about a method of the class
* in multiline
*/
public static void main(String[] args) {
}
Single Line comment in multiple line in Java
Normally we use // for short comment and /* */ for long comment or multiline. It’s upto you which you want to use for you code.
// Statement 1
// Statement 2
// Statement 3
What is an API documentation?
An API document is basically a .html file which contains details about all the features of an application, product, technology. By using API documentation gives the user an understanding of how to use the software and technology.
In Java, you should use a special compiler javadoc to generate the API documentation of the software.