HomeJava

Java for Testers – Print statements

Java for Testers – Print statements

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

In the previous article, I have explained the strictness of Java Syntax and Compiler Errors.

In this article, I am going to practically explain about the Print statements in Java as mentioned below:

  1. Demonstrating print statements
  2. Demonstrating println statements
  3. Difference between print and println statements
  4. Printing a number 

Let’s get started.

Java for Testers – Print statements

The purpose of Print statements in Java is used to print the program output to the console.

Print statements in Java can be categorized into the below two types:

  1. print
  2. println

Print Statements

Demonstration of print and println statement

Follow the below steps for practically understanding the print and println statements in Java:

1) The syntax of print statement looks like below:

2) 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

3) Inside the main() method, let’s write a Java print statement which prints the given text as shown below:

Print statements - created

4) Save the Project and click on the ‘Run’ button as shown below:

Print statements - run

5) Observe that the Program got executed and ‘Hello World!’  text inside the print statement got printed as output in the Console tab as shown below:

Print statements - output

6) Print statement print the text to output, but won’t move to the new line after printing (i.e. stays in the same line after printing)

To understand this, we have to create multiple print statements as shown below:

Let’s write  the above statement in the Java Project as shown below:

Print statements - multiple print

Save the Project and click on the ‘Run’ button as shown below:

Print statements - save and run

Observe that the Program got executed and all the print statements (i.e. 5 print statements) got printed in a single line as shown below:

Print statements - single line

7) println statements in Java also work in the same way by printing the given text to the output.

But there is a difference between print and println statements in Java.

print statements print the text and remain in the same line after printing.

Whereas println statements print the text and moves to the next line after printing.

In order to understand this, let’s replace all the multiple print statements in the above program with println statements as shown below:

Print statements - println

Save the Project and click on the ‘Run’ button as shown below:

Print statements - run2

Observe that the Program got executed and all the println statements (i.e. 5 println statements) got printed in multiple lines as shown below:

Print statements - output2

8) In order to print a text using print or println statements, we have to provide the text between the double quotes as shown below:

System.out.println(QAFox);

But, in order to print a number using Print statements, we don’t have to provide the double quotes as shown below:

System.out.println(9);

Now, let’s modify the Java program to print the text and number as shown below:

Print statements - text and number

Save the Project and click on the ‘Run’ button as shown below:

Print statements - save and run2

Observe that the Program got executed and the text & number got printed in the output as shown below:

Print statements - output3

Here concludes this article.

In the next article, I will explain the shortcut for typing System.out.println() statement in Eclipse IDE.

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

If you like this article, share it on LinkedIn using the below button:

Comments (0)

Leave a Reply

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