Last Updated : 20 Nov, 2024
print() and println() are the methods of System.out class in Java which are used to print the output on the console. The major difference between these two is that print() method will print the output on the same line while println() method will print the output on a new line.
Example:
Java
// Java program to show the difference between
// print() and println() method
import java.io.*;
class GFG {
public static void main(String[] args)
{
// using print method
System.out.print("Hello");
// using println method
System.out.println(" Geeks!");
// using print method
System.out.print("How are you?");
// using println method
System.out.println(" I'm fine.");
}
}
Hello Geeks! How are you? I'm fine.print() Method
print() method in Java is used to display a text on the console. This text is passed as the parameter to this method in the form of String. This method prints the text on the console and the cursor remains at the end of the text at the console. The next printing takes place from just here.
Example:
Java
// Java program to show the
// use of print method
import java.io.*;
class GFG {
public static void main(String[] args)
{
// The cursor will remain
// just after the 1
System.out.print("GfG1");
// This will be printed
// just after the GfG2
System.out.print("Gf2");
}
}
println() Method
println() method in Java is also used to display a text on the console. This text is passed as the parameter to this method in the form of String. This method prints the text on the console and the cursor remains at the start of the next line at the console. The next printing takes place from next line.
Example:
Java
// Java program to show the
// use of println method
import java.io.*;
class GFG {
public static void main(String[] args)
{
// The cursor will after GFG1
// will at the start
// of the next line
System.out.println("GfG1");
// This will be printed at the
// start of the next line
System.out.println("GfG2");
}
}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4