How to Print in Java
โก Smart Summary
Printing in Java relies on three core methods from the PrintStream class that send formatted text to the console, support different cursor behaviors, accept multiple data types through overloaded signatures, and allow precise output control for any developer requirement.

Java provides three core methods to send output to the console. Each method serves a distinct purpose and supports a range of data types through overloaded signatures, helping developers display information in the layout that best fits the program.
- Java print() method: Data displays on the same line using the print statement in Java.
- Java println() method: Output prints on the current line, and the cursor moves to the next line.
- Java printf() method: Data displays with a particular format as per requirement.
What Is the Java print() Method?
The Java print() method writes content to the console. It is an overloaded method of the Java PrintStream class and accepts a string as an input parameter.
After printing the input, the output cursor remains on the same line. The method works when no parameters are parsed. The print statement uses “\n” to move to the next line.
Syntax:
The syntax shows how to operate the Java print statement.
System.out.print(String input_data);
System.out is a Java expression that creates an instance of the PrintStream class. It represents the standard output stream. You cannot use the PrintStream object directly.
- System: The System is a final class used in the “java.lang” package to display data.
- out: The “out” is an instance of the Java System class with a public member field. It is a type of PrintStream package to create objects internally.
Example
The given example shows the working procedure of the print() method.
class HelloCoder{
public static void main(String[] args) {
System.out.print("How to Print in Java! \n");
System.out.print("Hello Coder!");
System.out.print("Guru99!");
}
}
Output:
How to Print in Java! HELLO CODER! Guru99
Why Use the Java println() Method?
The Java println() method is an advanced form of the print() method. It is used to show text on the console. It belongs to the PrintStream class through an overloaded method. This method works with a string parameter. After printing the given information, the cursor moves to the beginning of the next line.
Syntax:
The syntax shows how to operate the Java println statement.
System.out.println(String input_data);
println(): The PrintStream class used to display output on the console.
Example:
Let us see the example of the println() method.
public class HelloCoder {
public static void main(String[] args) {
System.out.println("How to Print in Java! ");
System.out.println("Hello Coder!");
System.out.println("Guru99!");
}
}
Output:
How to Print in Java! Hello Coder! Guru99!
Other Java Overloaded Methods in println()
Building on the println() method, the related print() method has the following overloaded signatures that accept different data types.
| Overloaded Methods | Print Data Format |
| print(boolean b) | This method shows the Boolean value |
| print(char c) | A method displays the character |
| print(char[] s) | It shows an array of characters |
| print(double d) | It displays a double-precision floating-point number |
| print(float f) | A method shows the floating-point number |
| print(long l) | It shows a long integer |
| print(int i) | It shows an integer number |
| print(Object obj) | This method operates on an object value |
| print(String s) | This method displays a string value |
How the Java printf() Method Works
The Java printf() method displays a formatted string on the console with a specific format. It is a Java overloaded method of the PrintStream class to print values. The specifiers are displayed with formatted arguments in the method.
Syntax:
The syntax shows how to operate the printf() statement.
System.out.printf(String display_format, Object input_data);
Example
Let us see the example of the printf() method.
public class HelloCoder{
public static void main(String[] args) {
System.out.printf("'%s' %n","How to Print in Java!");
System.out.printf("%S %n","Hello Coder!");
System.out.printf("'%S' %n","Learning");
}
}
Output:
'How to Print in Java!' HELLO CODER! 'LEARNING'
printf() Specifiers in Java
To control output formatting with System.out.printf, Java provides a set of specifiers that map each argument to the correct data type.
| Specifier | Type |
| %c | Character value |
| %d | Integer value |
| %f | Floating-point number |
| %s | String of characters |
| %% | Prints or displays a percent (%) sign |
| %e | Exponential floating-point value |
Example
Let us see an example that combines the print, println, and printf methods in Java.
public class HelloCoder{
public static void main(String[] args) {
System.out.println("How to Print in Java!");
System.out.printf("%S %n","Hello Coder!");
System.out.print(" Guru99");
}
}
Output:
How to Print in Java! HELLO CODER! Guru99
How to Print Using Entered Data in Java
Beyond fixed strings, Java can also print values entered by the user at runtime. The following steps show how to print entered data.
Step 1) Use the Java Scanner package to support user input data.
Step 2) A Scanner class or object operates to accept input from standard input. It enters through the keyboard.
Step 3) Create the variable using the “Scanner(System.in)” method.
Step 4) Use the variable. nextInt() then reads all values from the keyboard.
Step 5) This method gets input data until it encounters a new line character (Enter).
Step 6) Finally, the input value prints the normal output using the System.out.print() function.
Example
Let us see the example of entering user input data and printing it in Java.
import java.util.Scanner;
public class HelloCoder {
public static void main(String[] args) {
Scanner reader_data = new Scanner(System.in);
System.out.print("Enter a Value: ");
int number_data = reader_data.nextInt();
System.out.println("User entered: " + number_data);
int add = number_data+number_data;
System.out.println("Addition: " + add);
}
}
Output:
Enter a Value: 21 User entered: 21 Addition: 42
Other Java Overloaded Methods in print()
For reference, the print() method also supports the following overloaded signatures that accept different data types.
| Overloaded Methods | Print Data Format |
| print(boolean b) | This method shows the Boolean value |
| print(char c) | This method shows the character |
| print(char[] s) | This method displays an array of characters |
| print(double d) | A method shows a double-precision floating-point number |
| print(float f) | This method displays a floating-point number |
| print(long l) | This method shows a long integer |
| print(int i) | This method shows an integer number |
| print(Object obj) | It works for object value |
| print(String s) | This method operates on and displays string value |
Comparing print Statements in Java
To choose between System.out.print and System.out.println, the table below highlights the primary distinctions between the two methods in Java.
| Java println() statement | Java print() statement |
| The cursor displays on the next line after displaying the output on the console. We can interpret ‘ln’ in ‘println’ as the ‘next line’. | The cursor appears on the same line after displaying the output on the console using the print(). |
| We can use the System.out.println(data) function to get the data and show the cursor on the next line. | We can use the System.out.print(data) function to display the input data on the same line. |
