When looking at the source code for Integer.parseInt(String s, int radix)
(java 8, 1.8.0_131), I found the following comment block:
/*
* WARNING: This method may be invoked early during VM initialization
* before IntegerCache is initialized. Care must be taken to not use
* the valueOf method.
*/
While I understand the first part about the IntegerCache, I don't understand why there's a warning about valueOf
, and why in this context.
I see that valueOf()
relies on parseInt()
, but I still don't get why there's this warning.
Can someone explain what exactly the comment warns me about (and the context where valueOf shouldn't be used), and what could possibly go wrong.
Edit:
The code in Integer.valueOf(int i) seems to have changed since the other question from the comment below was asked, it is now
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
and should be save from the assertion error before.