Java 修改Integer變量值
對(duì)于Integer變量來(lái)說(shuō),比較變量值常見(jiàn)情形如下:
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;
當(dāng)我們對(duì)一個(gè)Interger變量直接用“=”號(hào)賦值時(shí),相當(dāng)于自動(dòng)裝箱,即把右邊的int型變量(整數(shù)時(shí)默認(rèn)是int) 轉(zhuǎn)換成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) //當(dāng)為-128和127之間時(shí),并沒(méi)有new一個(gè)Integer,而是從緩存中取 return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); } |
所以說(shuō)如果int的值為-128到127時(shí),是從常量池里取的Integer變量,所以“c==d”是對(duì)的,因?yàn)閏、d是100
而“a==b”為false,因?yàn)樗麄兊闹禐?000,所以是在堆里new出兩個(gè)不同的變量。
這些都很好理解,但是怎么改變Integer的整型值呢?
我嘗試了兩種方法去改變Integer的整型值
(1)
有這么一個(gè)方法
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); |
輸出的結(jié)果卻還是100而不是444
再來(lái)看看
(2)
main方法
1
2
3
|
Integer shuzi= new Integer( 100 ); shuzi= 5000 ; System.out.println(shuzi); |
這回輸出的結(jié)果卻又是5000,為什么(1)和(2)都用了“=”號(hào)賦值,一個(gè)成功,一個(gè)卻失敗了?
看看源碼
Integer的源碼:
1
2
3
4
5
6
|
/** * The value of the {@code Integer}. * * @serial */ private final int value; |
Integer變量里的value明明就是final變量,照理說(shuō)是不能夠修改的,那為什么(2)卻成功了?
因?yàn)椋涸冢?)的這個(gè)情況里,我對(duì)它賦值5000,相當(dāng)于new了一個(gè)新的Integer變量,它的value值是5000,然后把這個(gè)新的變量賦給“shuzi”這個(gè)變量,原來(lái)那個(gè)value值為100的Integer變量就被覆蓋了,除非我用一個(gè)副本保存,不然他就會(huì)被GC清除了。
還有一個(gè)問(wèn)題:那為什么(1)改變不了?
因?yàn)椋何覀兿纫溃琂ava 只有值傳遞,只不過(guò)值傳遞分為:內(nèi)存中數(shù)值的值傳遞以及內(nèi)存地址數(shù)值的值傳遞,傳遞一個(gè)Integer變量參數(shù)進(jìn)去,實(shí)際上是構(gòu)建了一個(gè)副本,通過(guò)這個(gè)副本我們只能去修改原來(lái)Integer變量的非final成員變量(假如有的話(huà),也可以是其他類(lèi)型),上面也說(shuō)了,如果去修改Integer類(lèi)型的final變量,那么是會(huì)新new一個(gè)Integer變量,去覆蓋這個(gè)變量副本,所以原來(lái)的Integer變量還是原來(lái)的,僅僅是“ceshi”這個(gè)方法里的副本變量變了,這么理解就清楚了。
Integer值比較需要注意的問(wèn)題
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類(lèi)型對(duì)于-128-127之間的數(shù)是緩沖區(qū)取的, * 所以用等號(hào)比較是一致的。但對(duì)于不在這區(qū)間的數(shù)字是在堆中new出來(lái)的。所以地址空間不一樣,也就不相等。 */ 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 ; // 裝箱過(guò)程也就是Integer b3=Integer.valueOf(60) System.out.println( "3:=" +(a3 == b3)); //false // System.out.println("3:="+(a3.equals(b3))); //true Integer a4 = 129 ; //大于127時(shí),在堆中新建 Integer b4 = 129 ; System.out.println( "4:=" +(a4 == b4)); //false // System.out.println("4:="+(a4.equals(b4))); //true } } |
代碼如上所示,運(yùn)行結(jié)果在注釋后。
原因
java中Integer類(lèi)型對(duì)于-128-127之間的數(shù)是緩沖區(qū)取的,所以用等號(hào)比較是一致的。但對(duì)于不在這區(qū)間的數(shù)字是在堆中new出來(lái)的。所以地址空間不一樣,也就不相等。
Integer b3=60,這是一個(gè)裝箱過(guò)程也就是Integer b3=Integer.valueOf(60)
解決辦法
使用 equals 代替 “==“,即是前者可能在性能上稍遜于后者,但是用后者的話(huà)存在bug的可能性。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/qiuwenjie123/article/details/80153206