In the previous tutorial, you learned to write your first Java program. Now, let's learn about Java comments.
Tip: We are introducing comments early in this tutorial series because, from now on, we will be using them to explain our code.
Comments are hints that we add to our code, making it easier to read and understand.
Comments are completely ignored by Java compilers.
For example,
class HelloWorld {
public static void main(String[] args) {
// print Hello World to the screen
System.out.println("Hello World");
}
}
Output
Hello World
Here, // print Hello World to the screen
is a comment in Java programming. The Java compiler ignores everything after the //
symbol.
Note: You can ignore the programming concepts and simply focus on the comments. We will revisit these concepts in later tutorials.
Single-line Comment
In Java, a single-line comment starts and ends in the same line. To write a single-line comment, we can use the //
symbol. For example,
// declare and initialize two variables
int a = 1;
int b = 3;
// print the output
System.out.println("This is output");
Here, we have used two single-line comments:
// declare and initialize two variables
// print the output
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,
/* This is an example of multi-line comment.
* The program prints "Hello, World!" to the standard output.
*/
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Output:
Hello, World!
Here, we have used the multi-line comment:
/* This is an example of multi-line comment.
* The program prints "Hello, World!" to the standard output.
*/
This type of comment is also known as Traditional Comment. In this type of comment, the Java compiler ignores everything from /*
to */
.
Prevent Executing Code Using Comments
While debugging there might be situations where we don't want some part of the code. For example,
public class Main {
public static void main(String[] args) {
System.out.println("some code");
System.out.println("error code");
System.out.println("some other code");
}
}
If we get an error while running the program, instead of removing the error-prone code, we can use comments to disable it from being executed; this can be a valuable debugging tool.
public class Main {
public static void main(String[] args) {
System.out.println("some code");
// System.out.println("error code");
System.out.println("some other code");
}
}
Why use Comments?
We should use comments for the following reasons:
- Comments make our code readable for future reference.
- Comments are used for debugging purposes.
- We can use comments for code collaboration as it helps peer developers to understand our code.
Note: Comments are not and should not be used as a substitute to explain poorly written code. Always try to write clean, understandable code, and then use comments as an addition.
In most cases, always use comments to explain 'why' rather than 'how' and you are good to go.
Next, we will be learning about Java variables and literals.