We use print in java to output required text directly to the console of IDE. Let’s see the syntax of print in java and the difference between print and println in Java.

Syntax of Print in Java:

Simple print statement:

1
System.out.print(Learning Java from SoftwareTestingMaterial);

A simple print statement with a new line:

1
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

1
2
3
4
5
6
7
8
9
10
11
12
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

1
2
3
4
5
6
7
8
9
10
11
12
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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