Java Comments
Java Comments
In Java, comments are used to explain code and to generate documentation. It can also be used to prevent execution when testing alternative code.
Single-line Comments
In Java, single-line comments start with two forward slashes //
.
Any text after the forward slashes //
is ignored by Java (It will not be executed).
In the following example, we will use a single-line comment before a line of code:
// This line is a comment, and Java ignores it
System.out.println("Hello World!");
Output
Hello World!
In the following example, we will use a single-line comment at the end of a line of code:
System.out.println("Hello World!"); // This is a comment
Output
Hello World!
Java Multi-line Comments
In Java, multi-line comments start with /*
and end with */
.
Any text between /*
and */
will be ignored by Java.
In the following example, we will use a multi-lice comment to explain the code:
/* This is the first line of a multi-comment
This is the second line. */
System.out.println("Hellow World!");
Note: Generally, we use
//
for short comments and/* */
for longer.