Comments are used to add explanatory notes to the source code that are not executed when the code is run. Comments are ignored by the compiler and do not affect the program’s functionality. They can be used to describe the purpose of a particular section of code, provide instructions for other developers working on the project, or disable code temporarily for debugging purposes. In Java, there are two types of comments: single-line comments and multi-line comments. Single-line comments begin with two forward slashes (//) and continue to the end of the line. Multi-line comments begin with a forward slash followed by an asterisk (/) and end with an asterisk followed by a forward slash (/), and can span multiple lines. It is good practice to add comments to code, as it makes the code more readable and understandable to others who may be working with it.
What Are Comments?
In computer programming, a comment is a programmer-readable explanation or annotation in the source code of a computer program. They are added with the purpose of making the source code easier for humans to understand, and are generally ignored by compilers and interpreters.
Comments are important as source code because of the following two reasons.
- In an organization, we work in a team; there are many programmers who work on the same project. So, the well-commented functions/logics are helpful to other programmers to understand the code better. They can easily understand the logic behind solving any problem.
- If you see/edit code later, comments may help you to memorize the logic that you have written while writing that code.
Commenting involves placing Human Readable Descriptions inside computer programs detailing what the Code is doing. Proper use of commenting can make code maintenance much easier, as well to find bugs faster. Further, commenting is very important when writing functions that other people will use.
How to use comments in Java Language?
There are basically three types of comments in Java Programming Language:
Single Line Comment
One-line comments in java are denoted with ‘//’.
Example
//text
The compiler ignores everything from // to the end of the line.
Multi-Line Comment
Multiline comments are started with ‘/*’ and ended with ‘*/’.
Example
/* text */
The compiler ignores everything from /* to */.
JavaDoc Comment
Documentation comments start with ‘/**’ and end with ‘*/’.
Example
/** documentation */
This is a documentation comment and in general, it’s called a doc comment. The JDK javadoc tool uses doc comments when preparing automatically generated documentation.
Single Or Multi-Line Comments
It is up to you which you want to use. Normally, we use // for short comments, and /* */ for longer.
public class CommentDemo{
public static void main(String[] args) {
// This is a single comment
System.out.println("Hello World");
/* The code below will print the words Hello World
to the screen, and it is amazing */
System.out.println("Hello World");
}
}
Output
Hello World
Hello World