Print In Java | Java Tutorial
Syntax of Print in Java:
Simple print statement:
System.out.print(“Learning Java from SoftwareTestingMaterial”);
A simple print statement with a new line:
System.out.println(“Learning Java from SoftwareTestingMaterial”);
What is the difference between print and println methods in Java?
The basic difference between the print() and println() methods in Java are when using println(), the cursor in the output will be shown in the next line after printing the required output on the screen whereas when using print() method, the cursor will be shown in the same line after printing the required output on the screen.
Sample Program:
Simple print program – Print text and stay in the same line
package classOneGeneral; public class Print { // Simple print program - Print text and go to new line public static void main(String [] args){ System.out.print("Learning Java from SoftwareTestingMaterial"); System.out.print("Learning Java from SoftwareTestingMaterial"); } }
Simple print program – Print text and go to a new line
package classOneGeneral; public class Print { // Simple print program - Print text and go to new line public static void main(String [] args){ // println adds a new line System.out.println("Learning Java from SoftwareTestingMaterial"); System.out.println("Learning Java from SoftwareTestingMaterial"); } }
Declaring a variable named website and print some text and go to a new line
package classOneGeneral; public class Print { // Declaring a variable named website and print some text and go to new line static String website = "SoftwareTestingMaterial"; public static void main(String [] args){ // Print text and go to new line System.out.println("Learning Java from "+website); // In above statement we used "+" to concatenate website with the given text } }
Must Read: Java Tutorial