Java Comment

In this lesson of our Java course, we will learn about the Java comments. Java comments are not part of the program. We add these comments to our code to make it more readable not only for our-self but for other programmers. In real life, multiple people will work on the same projects and it’s quite possible that your code may be checked / enhanced in the future by another programmer.

Java comments give us the flexibility to add more details to the Java program with no impact. As such, comments are not compiled into byte-code and completely ignored by the compiler while it’s compiling our code. In Java, we use // (two forward slashes) to start a comment. For example.

class Main {
    public static void main(String[] args) {
        // this is a comment
        System.out.println("Hello World");

If you run the above code, you will have the following output:

Hello World

If you see the // this is a comment is a comment and it’s completely ignored by the compiler.

Here are some more examples to of adding comment in Java

// This is a comment
// This is another comment
// Yet another comment

We can also place the comments after a statement like:

System.out.println("Hello User");    //It will say hello to user

1. Multiple Line Comments

In many cases, we may need to add over 1 line of comment to our code to ensure it’s providing more details and context to the other developer. To add multiple lines of comment, we can use /* .. */ in our program. This is like the above, except we don’t need to add // on each line. For Example.

class Main {
    public static void main(String[] args) {
        /* this is a multi line
         comment example. We can use it to add long comments 
         to our code */
        System.out.println("Hello World");

2. Why Use Java Comments?

As we mentioned earlier, there are many benefits of adding Java comments to our program.

  • Make code easier to read for other programmer.
  • Provide context and logic details to other developers.


In the lesson, we learned why to use comments in your Java program and what benefits they provide to other developers. As Always the source code for our entire series is available on the GitHub code repository.