Introduction
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
package test;
public class Test
{
public static void main(String args[])
{
System.out.print("first statement. ");
System.out.print("second statement. ");
System.out.print("third statement");
}
}
Output
println( ) method
The println( ) method prints the output in the next line of the previous result on the screen.
Example
package test;
public class Test
{
public static void main(String args[])
{
System.out.println("first statement.");
System.out.println("second statement.");
System.out.println("third statement.");
}
}
Output
Using Real Numbers and Integers in println( )
The following is a sample of using real numbers and integers in println( ):
package test;
public class Test
{
public static void main(String args[])
{
int x = 5;
int y = 10;
System.out.println(x+y);
System.out.println("x+y");
System.out.println("" + x + y);
System.out.println("5" + "10");
System.out.println(5 + 10 + x + y);
System.out.println("output " + (x + y));
System.out.println("output " + x + y);
}
}
Output
Concatenation in println( )
The following is a sample of concatenation in println( ):
package test;
public class Test
{
public static void main(String args[])
{
int x = 5;
String w = "wooooo";
System.out.println(x + " " + "yahooo " + w);
}
}
Output
Printing Characters Using println( )
The following is a sample of printing characters using println( ):
package test;
public class Test
{
public static void main(String args[])
{
char c = 67;
char d = 'C';
char e = 69;
char f = 'E';
System.out.println(c);
System.out.println(d);
System.out.println(e);
System.out.println(f);
}
}
Output
Escape Sequences in println( )
The following is a sample of Escape Sequences in println( ):
package test;
public class Test
{
public static void main(String args[])
{
int x = 5;
int y = 6;
int z = 7;
int m = 8;
System.out.println(x+"\n"+ y+"\n"+ z+"\t"+ m);
}
}
Output
Summary
This article explains the print( ) and println( ) methods in Java.