Print() and Println() Methods in Java

This article explains the print() and println() methods and their various uses in Java.when using println( ), in the output screen the cursor will be shown on the next line after printing the required output on the screen. But in print( ), in the output screen the cursor will be shown on the same line.

Java Print

 
Both of the print( ) and println( ) methods print data on the screen. Basically print( ) and println( ) are nearly the same with just very small difference between them. These methods are very popular in Java.
 

Differences Between print( ) and println( ) in Java

 
In Java, the print( ) and println( ) methods vary in the manner that, when using println( ), in the output screen the cursor will be shown on the next line after printing the required output on the screen. Basically we can understand 'ln' in 'println' as the 'next line'. But in print( ), in the output screen the cursor will be shown on the same line after printing the required output on the screen. This is the basic difference between the print( ) and println( ) methods.
 

print( ) method

 
The print() method prints the required output on the same line continuously again and again on the screen.
 
Example
  1. package test;  
  2. public class Test  
  3. {  
  4.     public static void main(String args[])  
  5.     {  
  6.         System.out.print("first statement. ");  
  7.         System.out.print("second statement. ");  
  8.         System.out.print("third statement");  
  9.     }  
  10. }  
Output
 
<>print( )

println( ) method

 
The println( ) method prints the output in the next line of the previous result on the screen.
 
Example
  1. package test;  
  2. public class Test  
  3. {  
  4.     public static void main(String args[])  
  5.     {  
  6.         System.out.println("first statement.");  
  7.         System.out.println("second statement.");  
  8.         System.out.println("third statement.");  
  9.     }  
  10. }   
Output
 
println( )
 

Using Real Numbers and Integers in println( )

 
The following is a sample of using real numbers and integers in println( ):
  1. package test;  
  2. public class Test  
  3. {  
  4.     public static void main(String args[])  
  5.     {  
  6.        int x = 5;  
  7.        int y = 10;  
  8.         System.out.println(x+y);  
  9.         System.out.println("x+y");  
  10.         System.out.println("" + x + y);  
  11.         System.out.println("5" + "10");  
  12.         System.out.println(5 + 10 + x + y);  
  13.         System.out.println("output " + (x + y));  
  14.         System.out.println("output " + x + y);  
  15.     }  
  16. }  
Output
 
<>real no and integers in println( )

Concatenation in println( )

 
The following is a sample of concatenation in println( ):
  1. package test;  
  2. public class Test  
  3. {  
  4.     public static void main(String args[])  
  5.     {  
  6.        int x = 5;  
  7.        String w = "wooooo";  
  8.        System.out.println(x + " " + "yahooo " + w);  
  9.     }  
  10. }  
Output
 
<>concatenation

Printing Characters Using println( )

 
The following is a sample of printing characters using println( ):
  1. package test;  
  2. public class Test  
  3. {  
  4.     public static void main(String args[])  
  5.     {  
  6.         char c = 67;  
  7.         char d = 'C';  
  8.         char e = 69;  
  9.         char f = 'E';  
  10.         System.out.println(c);  
  11.         System.out.println(d);  
  12.         System.out.println(e);  
  13.         System.out.println(f);  
  14.     }  
  15. }  
Output
 
<>printing characters

Escape Sequences in println( )

 
The following is a sample of Escape Sequences in println( ):
  1. package test;  
  2. public class Test  
  3. {  
  4.     public static void main(String args[])  
  5.     {  
  6.         int x = 5;  
  7.         int y = 6;  
  8.         int z = 7;  
  9.         int m = 8;  
  10.         System.out.println(x+"\n"+ y+"\n"+ z+"\t"+ m);  
  11.     }  
  12. }  
Output
 
escape sequences

Summary

 
This article explains the print( ) and println( ) methods in Java.