Why and How to Use Comments in Java

In this article we learn the comments in java, types of comments and why do we use comments.

Comments in Java (Java Comments)

The programmers can put the comments whenever they need to add documentation about a function, or any line defined within the program. The compiler does not read the comments while compiling and ignores them. The comments enhance code readability and understand ability of the program.

Types of Java Comments

The comments are of the following types:

1. Single-Line Comments in Java

2. Multi-Line Comments in Java

1. Single-Line Comments

The single-line comment is used to comment only one line of the code. It is the widely used and easiest way of commenting the statements.

Single line comments starts with two forward slashes (//). Any text in front of // is not executed by Java.


//This is single line comment.

2. Multi-Line Comments

The multi-line comment is used to comment multiple lines of code. It can be used to explain a complex code snippet or to comment multiple lines of code at a time (as it will be difficult to use single-line comments there).

Multi-line comments are placed between /* and */. Any text between /* and */ is not executed by Java.


/*This Is



comment. */

Why do we use comments in java?

Comments are used to make the program more readable by adding the details of the code.

It makes easy to maintain the code and to find the errors easily.

The comments can be used to provide information or explanation about the variable, method, class, or any statement.

It can also be used to prevent the execution of program code while testing the alternative code.

Other Popular Articles

Java Operators And Types of Operators In Java

Leave a Comment