Chapter 04 of 57

Comments in Java

When writing a program, sometimes we want to leave notes for ourselves or for other programmers. These notes can explain what a particular part of the code does, why we wrote it, or simply make the program easier to understand.

In Java, these notes are called comments.

The important thing about comments is that Java does not execute them. They are completely ignored when the program runs. They are only there to help people understand the code.

Why Do We Use Comments?

Imagine you write a large program today and come back to it six months later. You may look at some part of your code and think, "Why did I write this?" 😄

Comments can help you remember what different parts of your program are doing.

They are also useful when working in a team because another developer can understand your code more easily.

For example:

// Print the welcome message
System.out.println("Welcome to Java!");

The comment explains what the next statement is doing, but it has no effect on the program.

Single-Line Comments

The simplest type of comment in Java is a single-line comment.

We create it using two forward slashes:

// This is a comment

Everything written after // on that line is treated as a comment.

For example:

class Main {
    public static void main(String[] args) {

        // Print a message
        System.out.println("Hello World!");
    }
}

The output is:

Hello World!

The comment doesn't appear in the output because Java ignores it.

You can also put a comment after a statement:

System.out.println("Hello World!"); // Print a message

Here, Java executes the System.out.println() statement and ignores everything after //.

Multi-Line Comments

Sometimes we want to write a comment that takes more than one line. For this, Java provides multi-line comments.

They start with /* and end with */.

/*
   This is a multi-line comment.
   It can contain multiple lines.
*/

For example:

class Main {
    public static void main(String[] args) {

        /*
           Display a welcome message
           to the user.
        */
        System.out.println("Welcome to Java!");
    }
}

Everything between /* and */ is treated as a comment.

Comments Don't Affect the Program

Let's look at this example:

class Main {
    public static void main(String[] args) {

        // This line is ignored
        System.out.println("Hello");

        /*
           These lines are also ignored.
           Java doesn't execute them.
        */

        System.out.println("Java");
    }
}

The output will simply be:

Hello
Java

So you can think of comments as notes attached to your code. They are useful for humans but have no effect on the program's execution.

Don't Overuse Comments

Comments are useful, but that doesn't mean we should add a comment to every single line of code.

For example, this is unnecessary:

// Print Hello
System.out.println("Hello");

The code is already very easy to understand.

A better use of comments is to explain something that isn't immediately obvious:

// Apply the discount only for orders above ₹5,000
if (total > 5000) {
    discount = 10;
}

The comment provides useful context that may not be obvious just by looking at the code.