Java 修改Integer變量值
對于Integer變量來說,比較變量值常見情形如下:
1
2
3
4
5
6
|
Integer a = 1000 ; Integer b = 1000 ; Integer c = 100 ; Integer d = 100 ; System.out.println(a == b); System.out.println(c == d); |
“==”比較的是地址的值,所以正確答案是false,true;
當我們對一個Interger變量直接用“=”號賦值時,相當于自動裝箱,即把右邊的int型變量(整數時默認是int) 轉換成Integer變量,另外我們看看源碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
/** * Returns an {@code Integer} instance representing the specified * {@code int} value. If a new {@code Integer} instance is not * required, this method should generally be used in preference to * the constructor {@link #Integer(int)}, as this method is likely * to yield significantly better space and time performance by * caching frequently requested values. * * This method will always cache values in the range -128 to 127, * inclusive, and may cache other values outside of this range. * * @param i an {@code int} value. * @return an {@code Integer} instance representing {@code i}. * @since 1.5 */ public static Integer valueOf( int i) { assert IntegerCache.high >= 127 ; if (i >= IntegerCache.low && i <= IntegerCache.high) //當為-128和127之間時,并沒有new一個Integer,而是從緩存中取 return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); } |
所以說如果int的值為-128到127時,是從常量池里取的Integer變量,所以“c==d”是對的,因為c、d是100
而“a==b”為false,因為他們的值為1000,所以是在堆里new出兩個不同的變量。
這些都很好理解,但是怎么改變Integer的整型值呢?
我嘗試了兩種方法去改變Integer的整型值
(1)
有這么一個方法
1
2
3
|
public void ceshi(Integer integer){ integer= 4444 ; } |
main方法
1
2
3
|
Integer shuzi= new Integer( 100 ); new Test55().ceshi(shuzi); System.out.println(shuzi); |
輸出的結果卻還是100而不是444
再來看看
(2)
main方法
1
2
3
|
Integer shuzi= new Integer( 100 ); shuzi= 5000 ; System.out.println(shuzi); |
這回輸出的結果卻又是5000,為什么(1)和(2)都用了“=”號賦值,一個成功,一個卻失敗了?
看看源碼
Integer的源碼:
1
2
3
4
5
6
|
/** * The value of the {@code Integer}. * * @serial */ private final int value; |
Integer變量里的value明明就是final變量,照理說是不能夠修改的,那為什么(2)卻成功了?
因為:在(2)的這個情況里,我對它賦值5000,相當于new了一個新的Integer變量,它的value值是5000,然后把這個新的變量賦給“shuzi”這個變量,原來那個value值為100的Integer變量就被覆蓋了,除非我用一個副本保存,不然他就會被GC清除了。
還有一個問題:那為什么(1)改變不了?
因為:我們先要知道,Java 只有值傳遞,只不過值傳遞分為:內存中數值的值傳遞以及內存地址數值的值傳遞,傳遞一個Integer變量參數進去,實際上是構建了一個副本,通過這個副本我們只能去修改原來Integer變量的非final成員變量(假如有的話,也可以是其他類型),上面也說了,如果去修改Integer類型的final變量,那么是會新new一個Integer變量,去覆蓋這個變量副本,所以原來的Integer變量還是原來的,僅僅是“ceshi”這個方法里的副本變量變了,這么理解就清楚了。
Integer值比較需要注意的問題
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
|
package com.com.test; /** * Created by ***** 2018/6/29 9:18 * java中Integer類型對于-128-127之間的數是緩沖區取的, * 所以用等號比較是一致的。但對于不在這區間的數字是在堆中new出來的。所以地址空間不一樣,也就不相等。 */ public class IntegerTest { public static void main(String[] args) { Integer a1 = Integer.valueOf( 60 ); //danielinbiti Integer b1 = 60 ; System.out.println( "1:=" +(a1 == b1)); //true Integer a2 = 60 ; Integer b2 = 60 ; System.out.println( "2:=" +(a2 == b2)); //true Integer a3 = new Integer( 60 ); Integer b3 = 60 ; // 裝箱過程也就是Integer b3=Integer.valueOf(60) System.out.println( "3:=" +(a3 == b3)); //false // System.out.println("3:="+(a3.equals(b3))); //true Integer a4 = 129 ; //大于127時,在堆中新建 Integer b4 = 129 ; System.out.println( "4:=" +(a4 == b4)); //false // System.out.println("4:="+(a4.equals(b4))); //true } } |
代碼如上所示,運行結果在注釋后。
原因
java中Integer類型對于-128-127之間的數是緩沖區取的,所以用等號比較是一致的。但對于不在這區間的數字是在堆中new出來的。所以地址空間不一樣,也就不相等。
Integer b3=60,這是一個裝箱過程也就是Integer b3=Integer.valueOf(60)
解決辦法
使用 equals 代替 “==“,即是前者可能在性能上稍遜于后者,但是用后者的話存在bug的可能性。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/qiuwenjie123/article/details/80153206