当前位置: 代码网 > it编程>编程语言>Java > Java中Integer128的坑

Java中Integer128的坑

2025年03月21日 Java 我要评论
今天在学习java的时候遇到了下面几个问题。public static void main(string[] args) { integer num1 = 127; int

今天在学习java的时候遇到了下面几个问题。

public static void main(string[] args) {
        integer num1 = 127;
        integer num2 = 127;
        integer num3 = 128;
        integer num4 = 128;
        int num5 = 127;
        int num6 = 128;
        system.out.println(num1 == num2);
        system.out.println(num3 == num4);
        system.out.println(num1 == num5);
        system.out.println(num6 == num3);
    }

我会认为这个问题的输出结果是true、true、true、true。但是在运行的时候发现,结果并不是想象中的那样子。正确答案如下。

🌼一、integer和int的联系

java 5 引入了自动装箱(autoboxing)和拆箱(unboxing)机制,允许 int 类型和 integer 类型之间进行自动转换。

  • 自动装箱:将 int 类型的值自动转换为 integer 对象。
  • 自动拆箱:将 integer 对象自动转换为 int 类型的值。

1.1 integer和int的区别

1. 类型不同

  • int 是基本数据类型,直接存储整数值。

  • integer 是引用类型,存储的是对象的引用,指向堆内存中的 integer 对象。

2. 内存使用

  • int 变量在栈内存中直接存储整数值,占用的内存空间固定为 4 个字节。

  • integer 对象存储在堆内存中,除了存储整数值外,还需要额外的内存来存储对象的元数据,因此占用的内存空间相对较大。

3. 空值处理

  • int 是基本数据类型,不能为 null

  • integer 是引用类型,可以赋值为 null,这在某些需要表示 “无值” 的场景中非常有用。

1.2 integer和int的相互转换 

在上一篇文章当中说到了类和对象,integer并不是八大基本数据类型中的一个,integer是一个类,类似于上一篇文章说到的学生类。

int转换为intege:装箱

看下边一行代码。下边这行代码的执行涉及到了装箱操作。

integer num1 = 127;

代码真正在执行的时候会变为下方的样子。这就是装箱操作,就是把一个基本类型的int变量,包装成一个引用类型的integer变量。

integer num1 = integer.valueof(127);

integer转换为int:拆箱

下方的代码涉及到拆箱操作。

integer num1 = 127;
int num2 = num1;

代码在执行的时候会变为这个样子。这就是拆箱操作。

integer num1 = ineger.valueof(127);
int num2 = num1.intvalue();

🍎二、装箱

想要知道最开始的问题,就要具体了解,装箱到底是如何实现的,为什么对127装箱作比较是true,对128装箱后作比较是false,现在来跟进装箱的代码。

跟进到源码当中,我们发现这个方法对于我们传入的数值还进行了一次判断,i要和integercache的low和high作比较,如果在low和high之前,那么就返回一个值,否则的话就返回一个new出来的integer对象。

integercache.low

能够看到,low其实是一个值,大小为-128

integercache.high

同样的,high也是一个值,只不过好像没有初始值,这里我们先看做127。

对于一开始的问题,我们传进来的值是127。

 integer num1 = 127;
 integer num2 = 127;

 那么就会返回这个值。

 return integercache.cache[i + (-integercache.low)];

 integercache.cache其实是一个integer数组,用于存储一些integer类创建出的对象。

那么它可以有哪些对象呢?看如下代码。

static {
            // high value may be configured by property
            int h = 127;
            string integercachehighpropvalue =
                vm.getsavedproperty("java.lang.integer.integercache.high");
            if (integercachehighpropvalue != null) {
                try {
                    h = math.max(parseint(integercachehighpropvalue), 127);
                    // maximum array size is integer.max_value
                    h = math.min(h, integer.max_value - (-low) -1);
                } catch( numberformatexception nfe) {
                    // if the property cannot be parsed into an int, ignore it.
                }
            }
            high = h;

            // load integercache.archivedcache from archive, if possible
            cds.initializefromarchive(integercache.class);
            int size = (high - low) + 1;

            // use the archived cache if it exists and is large enough
            if (archivedcache == null || size > archivedcache.length) {
                integer[] c = new integer[size];
                int j = low;
                for(int i = 0; i < c.length; i++) {
                    c[i] = new integer(j++);
                }
                archivedcache = c;
            }
            cache = archivedcache;
            // range [-128, 127] must be interned (jls7 5.1.7)
            assert integercache.high >= 127;
        }

 这是一个静态初始块,现在不讲太多,只是integer类加载的时候会执行里边的代码,在这里有对cache的初始化。我们只需要关注下边的部分。

            int size = (high - low) + 1;

            // use the archived cache if it exists and is large enough
            if (archivedcache == null || size > archivedcache.length) {
                integer[] c = new integer[size];
                int j = low;
                for(int i = 0; i < c.length; i++) {
                    c[i] = new integer(j++);
                }
                archivedcache = c;
            }
            cache = archivedcache;

size就是我们设置的(127 + 128) + 1,也就是256。cache初始化的过程如下。

  • 根据low和high计算size
  • 初始化一个size大小的integer数组
  • 对数组进行赋值操作,也就是数组的0~255索引为存储的是-128~127之间的数字(包括0)

现在我们已经知道了,cache就是一个已经初始化好的数组,里边存储了-128~127之间的integer对象的引用。那么回到上访的代码。如果我们传入的值在low~high之间,那么就会直接从这个cache中拿取已经创建好的integer变量。

 return integercache.cache[i + (-integercache.low)];

现在有一点明白了,如果直接拿取的是已经创建好的对象,是不是就意味着每次拿的时候获取的都是同一个对象呢?就是这样子。

integer num3 = 128;
integer num4 = 128;

 如果传入的是128。那么就会通过new的方式来创建integer对象,每次new出来的是一个全新的对象,所以通过new方式创建的对象在怎么比较也是false,因为引用类型对象之间用==操作,比较的是两个对象的地址是否相同,也就是说num3和num4比较的是他们在内存空间的地址是否是相同的,并非比较的他们的内容是否都是128。

而num1和num2的比较其实也是比较地址,但是因为num1和num2指向的是同一个对象,所以就是true。

🌹三、拆箱

拆箱操作就很简单了,调用intvalue()方法返回包装的整数。

public static void main(string[] args) {
        integer num1 = 127;
        integer num2 = 127;
        integer num3 = 128;
        integer num4 = 128;
        int num5 = 127;
        int num6 = 128;
        system.out.println(num1 == num2);  // integer.valueof(127) == integer.valueof(127)
        system.out.println(num3 == num4);  // integer.valueof(128) == integer.valueof(128)
        system.out.println(num1 == num5);  // num1.intvalue() == 127
        system.out.println(num6 == num3);  // num2.intvalue() == 128
    }

现在是否对这个问题了解的更多了一点呢。

到此这篇关于java中integer128的坑的文章就介绍到这了,更多相关java integer128内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com