08.Java Comments
08.Java Comments
In this tutorial, you will learn about Java comments, why we use them, and
how to use comments in right way.
• single-line comment
• multi-line comment
Single-line Comment
A single-line comment starts and ends in the same line. To write a single-line
comment, we can use the // symbol. For example,
// "Hello, World!" program example
class Main {
public static void main(String[] args) {
// prints "Hello, World!"
System.out.println("Hello, World!");
}
}
Run Code
Output:
Hello, World!
The Java compiler ignores everything from // to the end of line. Hence, it is
also known as End of Line comment.
Multi-line Comment
When we want to write comments in multiple lines, we can use the multi-line
comment. To write multi-line comments, we can use the /*....*/ symbol. For
example,
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Run Code
Output:
Hello, World!
Note: In most cases, always use comments to explain 'why' rather than 'how'
and you are good to go.