盒装原语和等价

发布于 2024-08-17 06:18:34 字数 277 浏览 15 评论 0原文

所以今天有人问我这个问题。

Integer a = 3;
Integer b = 2;
Integer c = 5;
Integer d = a + b;
System.out.println(c == d);

这个程序会打印出什么?它返回 true。我回答说,由于我对自动(和自动取消)装箱的理解,它总是会打印出错误。我的印象是分配 Integer a = 3 将创建一个新的 Integer(3),以便 == 将评估引用而不是原始值。

谁能解释一下吗?

So I was asked this question today.

Integer a = 3;
Integer b = 2;
Integer c = 5;
Integer d = a + b;
System.out.println(c == d);

What will this program print out? It returns true. I answered it will always print out false because of how I understood auto (and auto un) boxing. I was under the impression that assigning Integer a = 3 will create a new Integer(3) so that an == will evaluate the reference rather then the primitive value.

Can anyone explain this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陌若浮生 2024-08-24 06:18:34

缓存 -128 到 127 之间的装箱值。装箱使用 Integer.valueOf 方法,该方法使用缓存。范围之外的值不会被缓存,并且始终创建为新实例。由于您的值属于缓存范围,因此使用 == 运算符使值相等。

引用Java语言规范:

如果装箱的值 p 为 true,
false,一个字节,一个范围内的字符
\u0000 到 \u007f,或者 int 或短整型
-128 到 127 之间的数字,然后令
r1 和 r2 是任意两个的结果
p的拳击转换。总是
r1 == r2 的情况。

http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.7

Boxed values between -128 to 127 are cached. Boxing uses Integer.valueOf method, which uses the cache. Values outside the range are not cached and always created as a new instance. Since your values fall into the cached range, values are equal using == operator.

Quote from Java language specification:

If the value p being boxed is true,
false, a byte, a char in the range
\u0000 to \u007f, or an int or short
number between -128 and 127, then let
r1 and r2 be the results of any two
boxing conversions of p. It is always
the case that r1 == r2.

http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.7

风筝在阴天搁浅。 2024-08-24 06:18:34

这就是实际发生的情况:

Integer c = Integer.valueOf(5);
Integer d = Integer.valueOf(a.intValue() + b.intValue());

Java 在 -128 到 127 之间维护 Integer 对象的缓存。与以下内容进行比较:

Integer a = 300;
Integer b = 200;
Integer c = 500;
Integer d = a + b;
System.out.println(c == d);

哪个应该打印 false

This is what is really happening:

Integer c = Integer.valueOf(5);
Integer d = Integer.valueOf(a.intValue() + b.intValue());

Java maintains a cache of Integer objects between -128 and 127. Compare with the following:

Integer a = 300;
Integer b = 200;
Integer c = 500;
Integer d = a + b;
System.out.println(c == d);

Which should print false.

§普罗旺斯的薰衣草 2024-08-24 06:18:34

这是因为一些(自动装箱的)整数被缓存,所以您实际上是在比较相同的引用 - 这篇文章有更详细的例子和解释。

It's because some of the (auto-boxed) Integers are cached, so you're actually comparing the same reference -- this post has more detailed examples and an explanation.

小清晰的声音 2024-08-24 06:18:34

缓存也发生在自动装箱之外,请考虑这一点:

Integer a = 1;
Integer b = new Integer(1);
Integer c = Integer.valueOf(1);

System.out.println(a == b);
System.out.println(b == c);
System.out.println(c == a);

这将打印:

false
false
true

通常在比较对象时您希望远离 ==

Caching happens outside of autoboxing too, consider this:

Integer a = 1;
Integer b = new Integer(1);
Integer c = Integer.valueOf(1);

System.out.println(a == b);
System.out.println(b == c);
System.out.println(c == a);

this will print:

false
false
true

Generally you want to stay away from == when comparing Objects

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文