In the previous article, I have explained the Shortcut for typing System.out.println() Java statement in Eclipse IDE.
In this article, I am going to explain and practically demonstrate using Comments in Java as mentioned below:
- Purposes of using Comments in Java
- Types of Comments in Java
Let’s get started.
Java for Testers – Using Comments
The main purpose of using the Comments in a Java program is to explain the underlined code.
Apart from using the comments for explaining the underlined code, Comments won’t be executed as other code in Java programs.
And also, if we don’t want any statement from getting executed, we can simply comment the required statement and Java will skip it from execution.
Types of Comments in Java:
There are two types of Comments in Java:
- Single Line
Demonstrating Comments in Java
Follow the below steps for practically implementing the above-said purposes of using Comments in Java programs:
1) Let’s create a Java program and create a Demo class as shown below:
Note: If you are not aware of creating a Java project and a Class in Java, refer to our previous article – Creating a Java project in Eclipse IDE
2) Inside the main() method, let’s type Print statement to print a text and a number as shown below:
3) Let’s use single-line comments first.
The following is the syntax for using single-line comments:
// Comments Text Here
As shown above, all the single-line comments in Java start with //
Now, let’s explain the underlying code in the Java program using single-line comments as shown below:
i.e. You can go through the comments provided in the below code and understand the code.
4) Comments provided in the Java program won’t be executed.
To find out, let’s Save the Project and click on the ‘Run’ button as shown below:
Observe that the Program got executed and the text & number got printed in the output and Comments didn’t get executed as shown below:
5) And if we don’t want any statement to be executed in Java, we can comment it out by simply specifying // before the line of code.
For example, in the above Java program, if you don’t want the print statement printing the number to be executed, we can simply comment the print statement by adding // before the statement as shown below:
To find out that the above-commented statement didn’t get executed, Save the Project and click on the ‘Run’ button.
Observe that the only text got printed and the number didn’t get printed as shown below:
This proves that the print statement for printing number got commented successfully and didn’t get executed.
6) If you have more text to be written as a comment and you want to write those text in more than one line, then we have to go with multi-line comments.
The below is the syntax for multi-line comments in Java:
/* This is a multi-line comment text
and it is written in more than one line
in the Java programs */
As shown above, the multi-line comments start with /* and end with */
Now, let’s explain the underlying code in the Java program using multi-line comments as shown below:
i.e. You can go through the multi-line comments provided in the below code and understand the code.
To find out that the above multi-line comment didn’t get executed, Save the Project and click on the ‘Run’ button.
Observe that the didn’t get executed as shown below:
Here concludes this article.
In the next article, I will explain about using Variables in Java programs.
- > To learn more about Java, continue to the next post (Click on Next Post link below)
- > Check complete Java Tutorial Contents here (Click here)
Please leave your questions/comments/feedback below:
Happy Learning 🙂
Arun Motoori (www.QAFox.com)
On a mission to help the Testing Community in all possible ways.