In this post, we will see about System.out.println in java and how it works.
Did you use system.out.println before? I think this is first statement every java developer compiles. It is generally called by the acronym "SOP".
What is System.out.println?
System.out.println is Java statement which prints argument passed to console.
out is static object of type PrintStream.
println is a method
Let’s go through more details.
System: System is class in the package "java.lang".Since java.lang package is imported into in every program by default,so java.lang package does not require an explicit import statement.
out: out is static member of System class and is an instance of java.io.PrintStream.Internally out object is connected to standard output stream of operating system, so any argument passed to out object goes to OS output stream.
println: println is method of PrintStream class.This method is overloaded to print message to output which is generally console or file.
So you can imagine structure as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class System { public static PrintStream out; // other code } class PrintStream { // method println public void println .. { } // other code } |
Does System.out.println impact performance?
Yes, System.out.println can impact performance if overused. When you call println, it internally calls write() and newLine(). Both write and newLine contain synchronized but have little overhead.
1 2 3 4 5 6 7 |
public void println(String x) { synchronized (this) { print(x); newLine(); } |
If multiple threads write at the same time, then performance can suffer but major cost is to add character to buffer and print them.
In any case, you should not have any System.out.println in the production environment.
That’s all about System.out.println in java.