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.

  • ๐Ÿ–จ๏ธ print method: System.out.print writes data on the same line, keeping the cursor in place for inline output.
  • ๐Ÿ“ค println method: System.out.println displays text and moves the cursor to the next line for clean separation.
  • ๐Ÿงพ printf method: System.out.printf applies format specifiers like %s, %d, and %f to control the output layout.
  • โœ… Overloaded signatures: Each method accepts boolean, char, int, long, float, double, String, and Object types for flexibility.
  • ๐Ÿงช User input: The Scanner class reads keyboard input through nextInt and prints the captured values with the print methods.

How to Print in Java

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.

  1. Java print() method: Data displays on the same line using the print statement in Java.
  2. Java println() method: Output prints on the current line, and the cursor moves to the next line.
  3. 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.

FAQs

System.out.print writes output on the same line and leaves the cursor in place. System.out.println writes the same output and then moves the cursor to a new line, which makes each statement appear on its own row.

Use System.out.printf when the output needs a specific layout, fixed width, or numeric precision. Specifiers such as %s, %d, and %.2f format strings and numbers, while println is better suited for simple one-line text output.

Use the plus operator to join a string with a variable inside System.out.println, for example System.out.println(“Total: ” + total). For richer formatting, use System.out.printf with specifiers like %s and %d to insert variable values into a template.

Yes, modern AI assistants in IDEs can suggest correct printf format strings, fix mismatched specifiers, and align arguments with their types. Developers should still review the suggested format to confirm width, precision, and locale behavior match the intended output.

In production code, AI assistants often recommend a logging framework such as SLF4J or java.util.logging instead of System.out.print. Logging supports levels, formatting, and output targets, while print methods remain useful for quick examples and small console programs.

Summarize this post with: