服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java修改Integer变量值遇到的问题及解决

Java修改Integer变量值遇到的问题及解决

2022-01-10 00:56丶老邱 Java教程

这篇文章主要介绍了Java修改Integer变量值遇到的问题及解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

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

延伸 · 阅读

精彩推荐