Internal working of System.out.println() in Java

By | September 26, 2023

In Java, System.out.println() is a statement that is used to print the arguments passed to it to the standard output, typically a console window.

Internal working of System.out.println() in Java
Fig 1- System.out.println()

Let us understand System.out.println in Java in detail:

System

System is a predefined final class present in the java.lang package. It is automatically imported in every Java program, we are not required to import it explicitly.

System class in Java
Fig 2- System.class

Can we create the object of the System class?

No, we cannot create the object of the System class. As we can see in the above picture (at line 113), it has a private constructor to restrict its initialization. This is the reason all the elements of the system class are static in nature.

out

out is an instance variable of PrintStream that is present as a static member variable of the System class.

PrintStream instance out in System.out.println
Fig 3- out: Instance variable of PrintStream type

As it is a static member variable, that’s why we call it using the class name and dot (.) operator as System.out, as shown in Fig-1.

println()

println() is a method of the PrintStream class; that’s why we are able to call it using the out variable, which is of the PrintStream type.

println method in Java
Fig 4- println() method

println() is an overloaded method of the PrintStream class.

Overloaded method println in Java
Fig 5- Overloaded method: println()

This is how System.out.println() works internally in Java.

Internal working of System.out.println() in Java
Fig 6- System.out.prinln()

How can we call the println() method on the PrintStream instance variable that is initialized as null, without giving the NullPointerException?

Internal working of System.out.println() in Java
Fig 7- PrintStream instance variable out

This is a very important interview question. As we see in above picture, the PrintStream instance variable out is initialized as null. How can we call the println() method on an object that is referenced to null without causing the NullPointerException?

The instance variable out is initialised internally by JVM. When the system class loads in memory, all of its static members are loaded, and it calls the setOut(PrintStream out) method, which takes an instance of PrintStream. i.e., out. This setOut(PrintStream out) method further calls the setOut0(PrintStream out) method and passes the out as an argument to it.

setOut method in Java
Fig 8- setOut(PrintStream out)

In the above picture, we can see that the setOut() method is calling the setOut0() method. If we look at the declaration of setOut0(), we can find that it is a native method.

Native methods in Java
Fig 9- Native methods

As we can see above, the methods are written with native keywords. And these methods are written in the C language, which is present in the System.c file. These methods are called by the Java JNI (Java Native Interface) and initialize the out instance. We can see the implementation of these native methods in the Java Official Documentations.

JNI in Java
Fig 10- setOut0(): Native method in System.c file

This native method initializes the instance variable; that’s why we do not get a NullPointerException when calling the println() method.

Leave a Reply

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