Javatpoint Logo
Javatpoint Logo

How to Print in Java

In Java, we usually use the println() method to print the statement. It belongs to the PrintStream class. The class also provides the other methods for the same purpose. In this section, we will learn how to print in Java. Along with this, we will also explain the statement System.out.println().

The method we should use depends on what we want to print and what type of output we want. There are following three methods to print the statements:

  • print() Method
  • println() Method
  • printf() Method

print() Method

The print() method is used to print text on the console. It is an overloaded method of the PrintStream class. It accepts a string as a parameter. After printing the statement, the cursor remains on the same line. It also works if we do not parse any parameter.

Syntax:

In the above method, if the argument is null, it prints the string null. When we pass a string as a parameter, the characters of the string are converted into bytes according to the platform's default character encoding, after that these bytes are written in the same manner of the write(int) method. The write() method writes the specified byte to the output stream.

The other overloaded methods of the print() method are:

Overloaded Method Prints
print(boolean b) A Boolean value
print(char c) A character
print(char[] s) An array of characters
print(double d) A double-precision floating-point number
print(float f) A floating-point number
print(long l) A long integer
print(int i) An integer
print(object obj) An object
print(String s)
A string

println() Method

It is an upgraded version of the print() method. It also used to display text on the console. It is an overloaded method of the PrintStream class. It accepts string as a parameter. After printing the statement, it throws the cursor at the starting of the next line. It is the main() difference between the println() and the print() method.

Syntax:

The above method first invokes the print(String) method and then println() method.

The other overloaded method of println() method are:

Overloaded Method Prints
print(boolean b) A Boolean value
print(char c) A character
print(char[] s) An array of characters
print(double d) A double-precision floating-point number
print(float f) A floating-point number
print(long l) A long integer
print(int i) An integer
print(object obj) An object
print(String s)
A string

printf() Method

The printf() method is used if we want to print the formatted string to the console using the specified format string and arguments. It is also an overloaded method of the PrintStream class. The method behaves the same as the invocation of the format() method.

Syntax:

It returns the output stream. It accepts two parameters:

format: It is a formatted String. If you want to know more about formatted String, go through the link https://bit.ly/2EaKzmq.

args: It is an argument referenced by the format specifiers. If the number of arguments is more than the format specifiers, the other arguments are ignored. The number of arguments may be zero.

It throws NullPointerExcepption if the format is null and also throws the IllegalFormatException if a format string contains illegal syntax.

The other overloaded method of the printf() method is:

  • printf(Locale l, String format, Object... args): It is used to write a formatted string to this output stream using the specified format string and arguments.

The problem with the above three methods is that we cannot directly use the methods. The reason is that we cannot create an object of the PrintStream class, directly. It means:

It is an invalid way to call the method. Java provides an alternative way to create an instance of the PrintStream class that is System.out. It represents the Standard Output Stream. It means that if we want to print any statement on the console, we should use the following statement:

Or

Where the parameter is whatever we want to print on the console.

How to Print in Java

Let's understand the meaning of the above print statements. In the above two statements, we observe that the statement is braked into three parts:

  • System: It is a final class that belongs to the java.lang.package.
  • out: The out is an instance of the System class and is of type PrintStream. It is a public and static member field. It is an instance of java.io.PrintStream. When we call the member, a PrintStream class object creates internally.
  • println(): It is the method of PrintStream class that is used to print statements on the console.

Let's use the above three methods in an example.

PrintDemo.java

Output:

122
A
Oracle
190.98
3.14
'javatpoint' 
'JACK'

Usually, we use the print() or println() method to print the statement. These methods are slow in performance because these are synchronized method. Therefore, multiple threads can lead to low-performance. It incurs heavy overhead on the machine in comparison to other I/O operations. The argument that we have parsed passed to the server's console. It requires the kernel time to execute the task and the kernel time refers to the CPU time.

We can also use the methods of the BufferedWriter class or PrintWriter class for performing the output. The performance of these class methods is fast in comparison to PrintStream class method.






Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA