Tuesday, December 23, 2014

In Java, how does System.out.println() work?

This might be the very basic java class discussion or question. Many of us know that, "System" is a class, "out" is an Object and "println" is method. But there might be an question like, how its really works?
This question is an excellent example of how just some very basic knowledge of Java can lead you to the correct answer. Most interviewers would not expect you to know the answer to do this right away – but would like to see how you think and arrive at an answer. In Java, the dot operator can only be used to call methods and variables so we know that ‘out’ must be either a method or a variable. Now, how do we categorize ‘out’? Well, ‘out’ could not possibly be a method because of the fact that there are no parentheses – the ‘( )’ – after ‘out’, which means that out is clearly not a method that is being invoked. And, ‘out’ does not accept any arguments because only methods accept arguments – you will never see something like “System.out(2,3).println”. This means ‘out’ must be a variable.

What is “out” in System.out.println()?

We now know that ‘out’ is a variable, so we must now ask ourselves what kind of variable is it? Variable could be a static or an instance variable. Because ‘out’ is being called with the ‘System’ class name itself, and not an instance of a class (an object), then we know that ‘out’ must be a static variable, since only static variables can be called with just the class name itself. So now we know that ‘out’ is a static variable belonging to the System class.

Is “out” in System.out.println() an instance variable?

As we already knows ‘println()’ is a method, we can further classify the ‘out’ in System.out.println(). We have already reasoned that ‘out’ is a static variable belonging to the class System. But now we can see that ‘out’ must be an instance of a class, because it is invoking the method ‘println()’. So 'out' variable must be an instance variable.

When and where is the “out” instantiated in System.out.println?

When the JVM is initialized, the method initializeSystemClass() will initializes the System class and sets the out variable. The initializeSystemClass() method actually calls another method to set the out variable – this method is called setOut().

The final answer to how system.out.println() works

The more exact answer to the original question is this: inside the System class is the declaration of ‘out’ that looks like: ‘public static final PrintStream out’, and inside the Prinstream class is a declaration of ‘println()’ that has a method signature that looks like: ‘public void println()’.
Here is what the different pieces of System.out.println() actually look like:

     //the System class belongs to java.lang package
     class System {
           public static final PrintStream out;
           //...
     }

     //the Prinstream class belongs to java.io package
     class PrintStream{
          public void println();
          //...
     }

No comments:

Post a Comment