HomeJava

Java for Testers – Using Comments

Java for Testers – Using Comments

<<Previous Post                    << Complete Tutorial>>                        Next Post>>

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:

  1. Purposes of using Comments in Java
  2. 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:

  1. Single Line
  2. Multi-Line

Using Comments Java - Types

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

Print statements - Java project

2) Inside the main() method, let’s type Print statement to print a text and a number as shown below:

Print Shortcut Eclipse IDE - print statements

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.

Using Comments Java - single line comments

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:

Using Comments Java - save and run

Observe that the Program got executed and the text & number got printed in the output and Comments didn’t get executed as shown below:

Using Comments Java - Didnt get executed

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:

Print Shortcut Eclipse IDE - commenting a statement

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:

Print Shortcut Eclipse IDE - only text printed

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.

Using Comments Java - multi line comments

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:

Print Shortcut Eclipse IDE - multi line didnt get executed

Here concludes this article.

In the next article, I will explain about using Variables in Java programs.

Next Steps:

  • > 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.

<<Previous Post                    << Complete Tutorial>>                        Next Post>>

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *

For FREE Testing Tutorials & Videos

X
Open chat
Contact Us on Whatsapp