加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 整数类对象

发布时间:2020-12-14 06:08:42 所属栏目:Java 来源:网络整理
导读:参见英文答案 Weird Integer boxing in Java????????????????????????????????????10个 我有一个代码片段: public class Test{ public static void main(String args[]){ Integer a = 100; Integer b = 100; Integer c = 5000; Integer d = 5000; System.ou

参见英文答案 > Weird Integer boxing in Java????????????????????????????????????10个
我有一个代码片段:

public class Test{
    public static void main(String args[]){
        Integer a = 100;
        Integer b = 100;
        Integer c = 5000;
        Integer d = 5000;

        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
        if(a == b)
            System.out.println("a & b Both are Equal");
        else
            System.out.println("a & b are Not Equal");
        if(c == d)
            System.out.println("c & d Both are Equal");
        else
            System.out.println("c & d are Not Equal");
    }
}

我不明白为什么输出是这样的?
输出是:
a& b两者是平等的
c& d不相等
我正在使用jdk1.7

最佳答案
这是由于虚拟机中的优化将小(常用)整数映射到重用的对象池. This answer解释了一些细节.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读