English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java flow control

Java array

Java object-oriented (I)

Java object-oriented (II)

Java object-oriented (III)

Java Exception Handling

Java List

Java Queue (queue)

Java Map collection

Java Set collection

Java input/output (I/O)

Java Reader/Writer

Other Java topics

Java Comments

In this tutorial, you will learn about Java comments, why to use them, and how to use them correctly.

In computer programming, comments are part of the program, and the Java compiler completely ignores comments. They are mainly used to help programmers understand the code more easily. For example,

//declare and initialize two variables
int a =1;
int b = 3;
//print output
System.out.println("This is output");

Here, we used the following comments:

  • declare and initialize two variables

  • print output

Types of comments in Java

In Java, there are two types of comments:

  • single-line comments

  • multiline comments

single-line comments

single-line comments start and end on the same line. To write a single-line comment, we can use//symbols. For example,

// "Hello, World!" program example
 
class Main {
    public static void main(String[] args) {}}    	
    
        // print output "Hello, World!"
        System.out.println("Hello, World!");
    

Output:

Hello, World!

Here, we used two single-line comments:

  • "Hello, World!" program example

  • print output "Hello World!"

The Java compiler will ignore from//all content from end of line to end of line. Therefore, it is also calledend of linecomments(single-line comments)

multiline comments

When we want to write multiline comments, we can use multiline comments. To write multiline comments, we can use/*....*/symbols. For example,

/* This is an example of multi-line comments.
 * The program will print “ Hello, World!” to the standard output.
 */
class HelloWorld {
    public static void main(String[] args) {}}    	
    	
        System.out.println("Hello, World!");
    

Output:

Hello, World!

In this case, we use multi-line comments:

/* This is an example of multi-line comments.
 * The program will print “ Hello, World!” to the standard output.
 */

This type of comment is also known asTraditional Comments/*to all the content*/

Correct Use of Comments

You should always know one thing, that is, comments should not be used as a substitute for poor code explanations. You should always write code that is well-structured and self-explanatory. Then, consider using comments.

Some people think that code should be self-explanatory and comments should be used sparingly. However, in my personal opinion, using comments is not wrong. We can use comments to explain complex algorithms, regular expressions, or solutions that require choosing a technology among different technologies to solve a problem.

Note: In most cases, it is always recommended to use comments to explain “ Why ” instead of “ How to do