How to print in Java with Examples: 3 Methods

Three methods or functions are provided in the Java language to print the output. These methods depend on the method requirement and desired output in coding.

  1. Java print() method: Data displays in the same line using print statement in java.
  2. Java println()Method: Output print in the current line, and cursor position moves to the next line.
  3. Java printf()Method: data displays with the particular format as per requirement.

Method 1: Java print() method

Java print method works for printing content to the console. It belongs to an overloaded method of the java PrintStream class. It takes a string as an input parameter.

The output cursor remains on the same line after printing the input statement. This method works if we do not parse any parameters. The print statement uses “\n” to go 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 method for creating an instance of the PrintStream class. It shows the Standard Output Stream. You cannot use the PrintStream object directly.

  • System: The System is a final class that is 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!

Method 2: Java println() method

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 the overloaded method. this method is worked with a string parameter. After printing the given information, the cursor is moved 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’s see the example of the print() 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 overload methods in println()

The print() method has the following overloaded methods:

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(doubled) It displays 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 object value
print(String s) This method displays string value

Method 3: Java printf() method

Java printf method displays the formatted string on the console with the 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’s see the example of the print() 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

The printf() method uses the following specifiers:

Specifier Type
%c Character value
%d Integer value
%f Floating number
%s string of characters
%% Print or display a percent(%) sign
%e exponential floating-point value

Example

Let’s see the example of the print method 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

How to print using entered data in Java

The following steps show us 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’s see the example of the enter user input data and print 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 overload methods in print()

The print() method has the following overloaded methods:

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(doubled) A method shows 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 and displays string value

Comparing print statements in Java

The primary distinction between the print() and println() methods in Java is shown below.

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 displays the input data on the same line.

Summary

  • The print statement is a simple way to display data for a Java programmer.
  • The System.out.print() function works with the three methods: print, println, and printf.
  • We can operate data in a single line and the next line format using the Java print methods.
  • The specifiers and overloaded method are used to display information with a required data type.
  • The Java printf() function uses specifiers as per user requirements. The print() and println() methods use overloaded methods.
  • The input data operates and displays using the scanner package and method.