亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java修改Integer變量值遇到的問題及解決

 更新時間:2021年09月17日 17:18:00   作者:丶老邱  
這篇文章主要介紹了Java修改Integer變量值遇到的問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

Java 修改Integer變量值

對于Integer變量來說,比較變量值常見情形如下:

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型變量(整數(shù)時默認是int) 轉(zhuǎn)換成Integer變量,另外我們看看源碼

/** 
         * 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)

有這么一個方法

public void ceshi(Integer integer){
    integer=4444;
}

main方法

Integer shuzi=new Integer(100);
new Test55().ceshi(shuzi);
System.out.println(shuzi);

輸出的結(jié)果卻還是100而不是444

再來看看

(2)

main方法

Integer shuzi=new Integer(100);
shuzi=5000;
System.out.println(shuzi);

這回輸出的結(jié)果卻又是5000,為什么(1)和(2)都用了“=”號賦值,一個成功,一個卻失敗了?

看看源碼

Integer的源碼:

/**
 * 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 只有值傳遞,只不過值傳遞分為:內(nèi)存中數(shù)值的值傳遞以及內(nèi)存地址數(shù)值的值傳遞,傳遞一個Integer變量參數(shù)進去,實際上是構(gòu)建了一個副本,通過這個副本我們只能去修改原來Integer變量的非final成員變量(假如有的話,也可以是其他類型),上面也說了,如果去修改Integer類型的final變量,那么是會新new一個Integer變量,去覆蓋這個變量副本,所以原來的Integer變量還是原來的,僅僅是“ceshi”這個方法里的副本變量變了,這么理解就清楚了。

Integer值比較需要注意的問題

package com.com.test;
 
/**
 * Created by ***** 2018/6/29 9:18
 * java中Integer類型對于-128-127之間的數(shù)是緩沖區(qū)取的,
 *  所以用等號比較是一致的。但對于不在這區(qū)間的數(shù)字是在堆中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
    }
}

代碼如上所示,運行結(jié)果在注釋后。

原因

java中Integer類型對于-128-127之間的數(shù)是緩沖區(qū)取的,所以用等號比較是一致的。但對于不在這區(qū)間的數(shù)字是在堆中new出來的。所以地址空間不一樣,也就不相等。

Integer b3=60,這是一個裝箱過程也就是Integer b3=Integer.valueOf(60)

解決辦法

使用 equals 代替 “==“,即是前者可能在性能上稍遜于后者,但是用后者的話存在bug的可能性。

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論