的當在源代碼中尋找Integer.parseInt(String s, int radix)
(Java 8,1.8.0_131),我發現下面的註釋塊:爲什麼parseInt函數警告使用的valueOf
/*
* WARNING: This method may be invoked early during VM initialization
* before IntegerCache is initialized. Care must be taken to not use
* the valueOf method.
*/
雖然我瞭解了IntegerCache第一部分,我不明白爲什麼有關於valueOf
的警告,以及爲什麼在這種情況下。
我看到valueOf()
依賴於parseInt()
,但我仍然不明白爲什麼會有這個警告。
有人可以解釋評論警告我什麼(以及不應該使用valueOf的上下文)什麼,以及可能出現什麼問題。
編輯:
在Integer.valueOf代碼(int i)以自似乎從下面的評論的另一個問題已經改變了有人問,現在是
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
,應該從節約之前的斷言錯誤。
可能重複[爲什麼在Integer類的Integer.valueOf方法中使用斷言?](https://stackoverflow.com/questions/33891156/why-is-assert-used-in-the-integer- valueof-method-of-integer-class) – Blasanka
assert似乎並未在java8 Integer中使用。valueOf()@Blasanka,你可以解釋一下重複的投票嗎,或者指向那些仍然等於在僞指針中鏈接的代碼的代碼? – Michael