Professional Documents
Culture Documents
Java 1
Java 1
SJEFF
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.
Table of Contents
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 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!");
}
}
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.
println(): The PrintStream class used to display output on the console.
Example:
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.
Example
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
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");
}
}
How to print using entered data in Java
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);
}
}
Other java overload methods in print()
• 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.