System.out.println : So called SOP/sysout

System.out.println : So called SOP/sysout

Widely used statement in Java and interestingly its one of the most number of times compiled statement in the history of java, also commonly asked interview question.

Here we will try to understand some interesting fact about sysout.

Inside System Class  (Check with JDK Source CodeJava Doc

As you can see out is a static member of System Class , now you can understand the significance of System.out and println (member function of PrintStream Class).

How Println Looks. (Note : println is overloaded with different input arguments check with jdk source code)

As you can see the println Method is synchronized. That’s why you shouldn’t write systouts in your code otherwise you will hit the performance.

Lets see why this method is synchronized.
If two thread contains:

Then without Synchronization the output may looks below

Lets understand the output steam.

as you can see out is assigned to nullPrintStream(), so how it will write to console.

Default output stream is set as below within the System Class (method : initializeSystemClass )

Brain Work :

As you have seen there is a method to set the output stream. So you can now change the default output stream..

Below program will change the default output stream to FileOutput Stream. i.e. whenever you write something to sysout the output will go to a file instead of console.

Enjoy 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.