Java has a comment function as well, and if you enclose a single line with [//], or multiple lines with [/*] and [*/], it will be treated as a comment.
It should be enough knowledge for you, but this page introduces comments in detail. I hope you will find it helpful.
2 Types and Formats of Comments
- Single-line comment format
- Multi-line comment format
1 What is the comment?
Comments can be written in programs, not only in Java. A comment is a part of a program source written but not executed.
There are two significant ways to use comments.
- To put notes about the program.
- To disable a part of a program to fix a bug, etc.
The former is used to write down what the program is and why it was written the way it was so that it is easy for others and your future self to see and understand.
The latter is often used, for example, to catch bugs. The term [commenting out] is used to comment out a program to not be processed, so keep this term in mind.
2 Types and Formats of Comments
There are two ways to comment: a Single-line comment or a multi-line comment.
Single-line comment format
A Single-line comment can be written as follows.
// (comment)
Sample program
Execution Result
This is a simple program that outputs a two-line string. Let's comment out the first line of this program that outputs a string.
Sample program
Execution Result
You can see that the first line is commented out, so it is no longer Output.
Multi-line comment format
Next, let's look at how to comment on multiple lines. A multi-line comment can be written as follows.
/*
comment
*/
Now, let's try commenting on the previous sample program in this way.
Sample program
Execution Result
If you comment out the second line of string output in this way, you will see that the second line is no more extended Output.
This method is convenient because it allows you to comment out multiple lines at once. Still, it can also lead to mistakes, such as commenting out sections that should not be commented out.
Sample program
Look at this sample program example: you are trying to comment out the string output on the second line, but you can see that the end of the comment [*/] is in the wrong place.
It is rare to make a mistake in a simple program like this. Still, when commenting out a program with complicated conditional expressions, it is possible to make a mistake, so be careful.
3 Usefulness of Comments
Since comments are not processed at program execution time, why not write them? You may think that this is a good idea, but writing comments can enhance the readability of a program.
Let's look at a simple example.
Even a beginner who has just begun to learn Java can somewhat understand this program at a glance.
However, by writing comments, the program can be understood faster. Let's try to describe comments in the sample program shown above.
In this way, the process can be easily understood by describing the process in comments. A program should not only be understood by the person who created it, but it should also be easy for other engineers to understand. Comments can also help other engineers understand the program.
Also, by writing information such as the creation date, creator, and version at the beginning of the class, the history of the class file itself can be understood.
Remember that the program content itself is important, of course, but the comments are also important.
4 Summary
This page has provided a summary of Java comments. The [//] and [/* */] can be used to make comments.
[//] is suitable for checking one line at a time or explaining the program line by line, while [/* */] is suitable for debugging a large batch or explaining the program at the beginning of a line.
Let's make good use of it to realize an easy-to-read program.
Java Book for Beginner
The most important selling points of Head First Java is its simplicity and super-effective real-life analogies that pertain to the Java programming concepts.
It is also the best book to learn java and start your learning journey with Java Development.
Head First Java covers almost all OOPS concepts and fascinatingly explains them.
Despite several readers claiming it to be a dated book, as it covers nothing beyond Java 5.0, Head First Java is still found on the shelves of numerous Java veterans.
Thus, it is a must-have book for every Java pursuer and developer.