Looking at
http://www.docjar.com/html/api/java/lang/Integer.java.html
I see this comment
/*
* WARNING: This method may be invoked early during VM initialization
* before IntegerCache is initialized. Care must be taken to not use
* the valueOf method.
*/
If they had used valueOf
in parseInt
then when parseInt
is called early in VM initialization, the high
value would actually be zero.
I am not sure what problems that would cause, but evidently someone thought it was worth guarding against.
- what is the use of assert IntegerCache.high >= 127; I read that assert provides an effective way to detect and correct programming errors. But this is runtime code why someone will use assert?
To guard against code invoked during VM initialization calling the valueOf method.
- And when it will throw AssertionError in this scenario?
If you run java with the -ea option (enabling assertions) and there is some initialization code that calls valueOf the JVM will almost immediately crash because of AssertionError. Hopefully the Java team tests for this stuff and will fix the offending code before releasing that to the wild.
Contrary to other answer's assertions that high will always be greater than or equal to 128, it appears that there are times it is zero (not counting reflection).