這是一個挺有意思的討論話題。
如果你運行下面的代碼
1
2
3
4
|
Integer a = 1000 , b = 1000 ; System.out.println(a == b); //1 Integer c = 100 , d = 100 ; System.out.println(c == d); //2 |
你會得到
false
true
基本知識:我們知道,如果兩個引用指向同一個對象,用==表示它們是相等的。如果兩個引用指向不同的對象,用==表示它們是不相等的,即使它們的內容相同。
因此,后面一條語句也應該是false 。
這就是它有趣的地方了。如果你看去看 Integer.java 類,你會發現有一個內部私有類,IntegerCache.java,它緩存了從-128到127之間的所有的整數對象。
所以事情就成了,所有的小整數在內部緩存,然后當我們聲明類似——
1
|
Integer c = 100 ; |
的時候,它實際上在內部做的是
1
|
Integer i = Integer.valueOf( 100 ); |
現在,如果我們去看valueOf()方法,我們可以看到
1
2
3
4
5
|
public static Integer valueOf( int i) { if (i >= IntegerCache.low && i return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); } |
如果值的范圍在-128到127之間,它就從高速緩存返回實例。
所以…
1
|
Integer c = 100 , d = 100 ; |
指向了同一個對象。
這就是為什么我們寫
1
|
System.out.println(c == d); |
我們可以得到true。
現在你可能會問,為什么這里需要緩存?
合乎邏輯的理由是,在此范圍內的“小”整數使用率比大整數要高,因此,使用相同的底層對象是有價值的,可以減少潛在的內存占用。
然而,通過反射API你會誤用此功能。
運行下面的代碼,享受它的魅力吧
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException { Class cache = Integer. class .getDeclaredClasses()[ 0 ]; //1 Field myCache = cache.getDeclaredField( "cache" ); //2 myCache.setAccessible( true ); //3 Integer[] newCache = (Integer[]) myCache.get(cache); //4 newCache[ 132 ] = newCache[ 133 ]; //5 int a = 2 ; int b = a + a; System.out.printf( "%d + %d = %d" , a, a, b); // } |
英文原文:Why 1000 == 1000 Returns False, but 100 == 100 Returns True in Java?
到此這篇關于淺談為什么Java中1000==1000為false而100==100為true的文章就介紹到這了,更多相關Java 1000==1000為false 內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:http://www.codeceo.com/article/why-java-1000-100.html