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:
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.
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:
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.
The above method first invokes the print(String) method and then println() method.
The other overloaded method of println() method are:
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.
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:
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:
Where the parameter is whatever we want to print on the console.
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:
Let's use the above three methods in an example.
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.