<<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:
- Demonstrating print statements
- Demonstrating println statements
- Difference between print and println statements
- 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:
- println
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:
System.out.print("Hello World!");
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
3) Inside the main() method, let’s write a Java print statement which prints the given text as shown below:
System.out.print("Hello World!");
4) Save the Project and click on the ‘Run’ button as shown below:
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:
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:
System.out.print("One"); System.out.print("Two"); System.out.print("Three"); System.out.print("Four"); System.out.print("Five");
Let’s write the above statement in the Java Project as shown below:
Save the Project and click on the ‘Run’ button as shown below:
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:
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:
Save the Project and click on the ‘Run’ button as shown below:
Observe that the Program got executed and all the println statements (i.e. 5 println statements) got printed in multiple lines as shown below:
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:
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 as shown below:
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>>