Java Comments

Comments can be used to describe Java code, making it more readable. It may also be used to prevent usage when testing another code.


Single-line Comments

One-line comment starts with two forward slash (//).

Any text between // and end of line is ignored by Java (will not be used).

This example uses comments of one line before the line of code:


Example
// This is a comment
            System.out.println("Hello World");
            

This example uses a single line comment at the end of the code line:


Example
System.out.println("Hello World"); // This is a comment
        


Java Multi-line Comments

Multi-line comments start with /* and end with */.

Any text between /* and */ will be ignored by Java.

This example uses multi-line comments (comment block) to define code:


Example
/* The code below will print the words Hello World
            to the screen, and it is amazing */
            System.out.println("Hello World");
            

Single or multi-line comments

It is up to you to decide. Generally, we use // short, and /* */ short comments.