JAVA中Integer對象的引用
JAVA中沒有指針一說,但也有引用的概念。這里要說的主要是Integer是不是同一個對象。
1、先看一段代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
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、通過上面描述,那么如果改成這樣又是什么答案
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
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、那么再改一下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
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的操作,是不進行裝箱的,而是在堆中生成對象的。
理解了裝箱、緩存、引用就不難理解了??梢宰约涸囋嚒?/p>
先來點基礎的知識
1
2
3
4
5
6
7
8
9
|
基本類型和包裝類的對應 byte Byte short Short int Integer long Long float Float double Double char Character boolean Boolean |
上述的八中基本數據類型的對應關系只有 int->Integer char->Character 兩個變化較大,其余都只是將首字母轉換為小寫。
再來了解一下JDK5的新特性:自動裝箱和拆箱
自動裝箱:把基本類型轉換為包裝類類型
自動拆箱:把包裝類類型轉換為基本類型
1
2
3
4
5
6
7
8
9
10
11
12
|
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; //自動拆箱,把對象轉換為基本數據類型 } } |
注意事項
1
2
3
4
5
6
7
|
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); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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( "-----------" ); } } |
1
2
3
4
5
6
7
|
Output: false true ----------- false true ----------- |
原因:
new 是在堆內存開辟空間的,自然比較地址值(==)都為false.
由于Integer重寫了equals方法,所以equals輸出都為true.
你可能感覺太簡單了,沒有任何技術含量,因為上面的不是重點,看下面代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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( "-----------" ); } } |
1
2
3
4
5
6
7
|
Output: true true ----------- false true ----------- |
原因:
為什么當int大于127就是兩個對象,127這個數字是不是覺得很熟悉?
-128到127是byte的取值范圍,如果在這個取值范圍內,自動裝箱就不會創建新對象了,而從常量池中獲取
超過了byte的取值范圍就會在創建新對象
自動裝箱其底層會調用valueOf()方法,簡單源碼分析(JDK1.8):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
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個階段。而解析階段即是虛擬機將常量池內的符號引用替換為直接引用的過程。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:http://blog.csdn.net/wang_1997/article/details/52275114