System.out.println in Java

Archit Pandita
2 min readDec 15, 2020

--

Know everything about System.out.println() in this article.

Before covering what is System.out.println() in java.Tell me how do you access static variable. Yes it can be accessed directly by the class name and doesn’t need any object.

Syntax:<class-name>.<variable-name>

Now see this code snippet:

name is static instance of string class inside Person class and length() is method of String class.

Now compare this snippet with System.out.println

Now what do you observe??

out is static instance of PrintStream class inside System class and println() is method of PrintStream class.

System is final class present in java.lang package.out is static instance inside System class of type PrintStream. println() is method present in PrintStream class.

In addition to it PrintStream has around 10 different overloads of println() method that are invoked based on the type of parameters passed by the user.For example:

System.out.println(), 
System.out.println(int),
System.out.println(double),
System.out.println(string),
System.out.println(character),
etc.

Important Question asked by interviewer.

Difference between System.out.print() and System.out.println():

System.out.print(): This method prints the text on the console and the cursor remains at the end of the text at the console. The next printing takes place from just here. This method must take atleast one parameter else it will throw an error.

System.out.println(): This method prints the text on the console and the cursor remains at the start of the next line at the console. The next printing takes place from the next line. This method may or may not take any parameter.

Performance Analysis of System.out.println():

println() is a method that helps display output on a console. This might be dependent on various factors that drives the performance of this method. The message passed using println() is passed to the server’s console where kernel time is required to execute the task. Kernel time refers to the CPU time. Since println() is a synchronized method, so when multiple threads are passed could lead to the low-performance issue. System.out.println() is a slow operation as it incurs heavy overhead on the machine compared to most IO operations.

There is an alternative way of performing output operations by invoking PrintWriter or the BufferedWriter class.

They are fast as compared to the println() of the PrintStream class.

--

--

Archit Pandita
Archit Pandita

Written by Archit Pandita

Software Engineer @ Payment Industry

No responses yet