Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the nullPrintStream() function in java/lang/System compare currentTimeMillis() to zero?

Tags:

java

jvm

When loading the System class, the <clinit> method instantiates the in, out and err PrintStream variables to null using the nullPrintStream() method:

private static PrintStream nullPrintStream() throws NullPointerException {
    if (currentTimeMillis() > 0) {
        return null;
    }
    throw new NullPointerException();
}

I understand why this is the case, and why the variables cannot be instantiated during loading, but what I'm confused about is the content of that method.

Why is it comparing currentTimeMillis() to 0? In what case would that comparison ever return false?

like image 839
Jivings Avatar asked Jan 18 '12 17:01

Jivings


1 Answers

The Javadoc for the nullPrintStream() method gives a clue:

The compiler, however, cannot be permitted to inline access to them, since they are later set to more sensible values by initializeSystemClass().

This is a coding hack, I guess, to prevent the compiler from inlining a simple "return null" implementation.

currentTimeMillis() will never be less than 0. But the compiler isn't clever enough to know that, and therefore leaves the conditional statement intact.

like image 138
Steve McLeod Avatar answered Oct 14 '22 14:10

Steve McLeod