Welcome back to Java Code Online. This is a small article on the type of comments available in Java. A comment is line in the Java Program which is not executed by the JVM. The syntax of the comment tells the JVM, that this line or these lines are not to be compiled and these lines of code need to be skipped.
Comments help in Java code by making the code clearer, providing a mechanism to maintain a history in the java files, and helps the developer in understanding what the code is doing. History is helpful in telling the developer that what is this class for, and what was the last time it was updated. Comments are an essential component of Java, and must be taken into practice, if you want to make your and others life easy.
Suppose that you write a code for making a dog Object. After around a year or so, there is a change requirement or some bug found in the code. Now you come back to the same code, but you can't remember what was the functionality of the code that you have written one year back. In this case comments help the developer remember the functioning of the code.
Java has two types of comments possible in its code. They are:-
1. Single line comment
2. Multiple line comment
Java provides you the option to comment a single line at a time, or to comment multiple lines at the same time.
For commenting a single line, use double forward slash(//) before the line you want to comment. For example:-
// The code below is for making a new Dog Object
Dog d = new Dog();
In the two lines of code written above, the first line will be skipped by the JVM, and will jump to the second line. The first line helps to tell the developer that the code below is for making a new Dog Object.
There are instances when you would like to comment multiple lines at one shot. Suppose that your comments span to multiple lines, then will you proceed by putting a "//" before every line. It would be tedious, so Java provides a different mechanism for this , the syntax for commenting multiple lines of Java is:-
/* The code is below for making a Dog Object
This code has the benefit that it simplifies the logic.
This is a multiple line comment.
And you are reading its syntax */
Dog d = new Dog();
Here everything in between "/*" and "*/" are skipped by the JVM compiler. So you can now comment multiple lines of code easily. If this article was beneficial then do leave a comment. For more info on Java, keep buzzing Java Code Online.