JAVA中Integer對象的引用
JAVA中沒有指針一說,但也有引用的概念。這裡要說的主要是Integer是不是同一個對象。
1、先看一段代碼:
public static void main(String[] args){ Integer a1 = 100; Integer b1 = a1;//另一種也可以b1=100 Field field = null; try { field = a1.getClass().getDeclaredField("value"); } catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } field.setAccessible(true); try { field.set(a1, 5000); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("b1="+b1); Integer c1 = 100; System.out.println("c1="+c1); }結果:
b1=5000
c1=5000
從上面,首先這裡要說明幾個,
1)、對於Integer來說,-128-127之間的整型已經初始化放在IntegerCache中,如果是裝箱的話,就會從這裡面取對象。
2)、b1=a1到底是數字賦值還是同一個對象?這個從結果實際就可以看出來,b1和a1指向同一個對象,而不是同一個數值
3)、c1=100,說明對於-128-127之間的數值,都是從IntegerCache中獲取的對象,100對應的Integer對像被改變後,後續對於100的裝箱都被改變。因為獲取cache中對象時用的是數組索引,而不是數值比較獲取的。
不過修改這個緩存會比較危險,不介意。誰知道什麼jar包或者什麼平台來個100的裝箱,但得到結果又不是100,到時就崩潰了。
2、通過上面描述,那麼如果改成這樣又是什麼答案
public static void main(String[] args){ Integer a1 = 200; Integer b1 = a1; Field field = null; try { field = a1.getClass().getDeclaredField("value"); } catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } field.setAccessible(true); try { field.set(a1, 5000); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("b1="+b1); Integer c1 = 200; System.out.println("c1="+c1); } 3、那麼再改一下
public static void main(String[] args){ Integer a1 = new Integer(100); Integer b1 = a1; Field field = null; try { field = a1.getClass().getDeclaredField("value"); } catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } field.setAccessible(true); try { field.set(a1, 5000); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("b1="+b1); Integer c1 = 100; System.out.println("c1="+c1); }這又是什麼答案。對於new的操作,是不進行裝箱的,而是在堆中生成對象的。
理解了裝箱、緩存、引用就不難理解了。可以自己試試。
先來點基礎的知識
基本類型和包裝類的對應byte Byte short Short int Integer long Long float Float double Double char Character boolean Boolean
上述的八中基本數據類型的對應關係只有int->Integer char->Character 兩個變化較大,其餘都只是將首字母轉換為小寫。
再來了解一下JDK5的新特性:自動裝箱和拆箱
自動裝箱:把基本類型轉換為包裝類類型
自動拆箱:把包裝類類型轉換為基本類型
public class Demo_Integer { public static void main(String[] args) { //JDK1.5之前int a = 100; Integer a1 = new Integer(a); //將基本數據類型包裝成對象,裝箱int b = a1.intValue(); //將對象轉換為基本數據類型,拆箱//JDK1.5之後int x = 100; Integer x1 = x; //自動裝箱,把基本數據類型轉換為對象int y = x1 + x; //自動拆箱,把對象轉換為基本數據類型} }注意事項
public class Demo_Integer { public static void main(String[] args) { Integer a = null; int b = a + 100; //自動拆箱底層將會調用a.intValue(),a為null,自然會拋出NullPointerException System.out.println(b); } }面試題
public class Demo_Integer { public static void main(String[] args) { Integer i1 = new Integer(97); Integer i2 = new Integer(97); System.out.println(i1 == i2); System.out.println(i1.equals(i2)); System.out.println("-----------"); Integer i3 = new Integer(197); Integer i4 = new Integer(197); System.out.println(i3 == i4); System.out.println(i3.equals(i4)); System.out.println("-----------"); } }Output: false true ----------- false true -----------
原因:
new 是在堆內存開闢空間的,自然比較地址值(==)都為false.
由於Integer重寫了equals方法,所以equals輸出都為true.
你可能感覺太簡單了,沒有任何技術含量,因為上面的不是重點,看下面代碼
public class Demo_Integer { public static void main(String[] args) { Integer i1 = 127; Integer i2 = 127; System.out.println(i1 == i2); System.out.println(i1.equals(i2)); System.out.println("-----------"); Integer i3 = 128; Integer i4 = 128; System.out.println(i3 == i4); System.out.println(i3.equals(i4)); System.out.println("-----------"); } }Output: true true ----------- false true -----------
原因:
為什麼當int大於127就是兩個對象,127這個數字是不是覺得很熟悉?
-128到127是byte的取值範圍,如果在這個取值範圍內,自動裝箱就不會創建新對象了,而從常量池中獲取
超過了byte的取值範圍就會在創建新對象
自動裝箱其底層會調用valueOf()方法,簡單源碼分析(JDK1.8):
public final class Integer extends Number implements Comparable<Integer> { public static Integer valueOf(int i) { //當i >= -128 且i <= 127 時,會直接將取緩衝區中的對象if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i);//超過了byte取值範圍會在堆內存創建} //內部類充當緩衝區private static class IntegerCache { static final int low = -128; static final int high; static final Integer cache[]; static { // high value may be configured by property int h = 127; String integerCacheHighPropValue = sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); if (integerCacheHighPropValue != null) { try { int i = parseInt(integerCacheHighPropValue); i = Math.max(i, 127); // Maximum array size is Integer.MAX_VALUE h = Math.min(i, Integer.MAX_VALUE - (-low) -1); } catch( NumberFormatException nfe) { // If the property cannot be parsed into an int, ignore it. } } high = h; cache = new Integer[(high - low) + 1]; int j = low; for(int k = 0; k < cache.length; k++) cache[k] = new Integer(j++); // range [-128, 127] must be interned (JLS7 5.1.7) assert IntegerCache.high >= 127; } private IntegerCache() {} } } 8種基本類型的包裝類和對像池
java中基本類型的包裝類的大部分都實現了常量池技術,這些類是Byte,Short,Integer,Long,Character,Boolean,另外兩種浮點數類型的包裝類則沒有實現。另外Byte,Short,Integer,Long,Character這5種整型的包裝類也只是在對應值小於等於127時才可使用對像池,也即對像不負責創建和管理大於127的這些類的對象
擴展知識
在jvm規範中,每個類型都有自己的常量池。常量池是某類型所用常量的一個有序集合,包括直接常量(基本類型,String)和對其他類型、字段、方法的符號引用。之所以是符號引用而不是像c語言那樣,編譯時直接指定其他類型,是因為java是動態綁定的,只有在運行時根據某些規則才能確定具體依賴的類型實例,這正是java實現多態的基礎。
在JVM中,類從被加載到虛擬機內存中開始,到卸載出內存為止,它的整個生命週期包括:加載、驗證、準備、解析、初始化、使用和卸載7個階段。而解析階段即是虛擬機將常量池內的符號引用替換為直接引用的過程。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對武林網的支持。