java中允许整数== int

发布于 2024-12-08 03:47:35 字数 201 浏览 0 评论 0原文

我想知道在与 int 比较时,java 是否会自动将 Integer 转换为 int ?或者 == 会尝试比较原语的引用吗?

这总是正确的还是我需要执行i.intValue()==2

Integer i = Integer.valueOf(2);
if (i==2){
//always?
}

I was wondering if java automatically turns a Integer into an int when comparing to an int? Or will the == try and compare references on primitives?

Is this always true or do I need to do i.intValue()==2?

Integer i = Integer.valueOf(2);
if (i==2){
//always?
}

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

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

发布评论

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

评论(6

帅的被狗咬 2024-12-15 03:47:35

是的,当使用 == 比较 int 时,如有必要,参数将被拆箱。

Java 语言规范中的相关部分:

15.21.1 数值相等运算符 == 和 !=

如果等式运算符的操作数均为数字类型,或者其中一个为数字类型,另一个可转换(第 5.1.8 节)为数字类型,则对操作数执行二进制数字提升(第 5.6.2 节) )。如果操作数的提升类型为 int 或 long,则执行整数相等测试;如果提升的类型是 float 或 double,则执行浮点相等测试。

请注意,二进制数字提升执行值集转换(第 5.1.13 节)和拆箱转换(第 5.1.8 节)。无论浮点值的代表值来自哪个值集,都可以准确地进行比较。

同样适用于 <<=>>= 等,以及 <代码>+、<代码>-、<代码>*等。

因此,

System.out.println(Integer.valueOf(17) == 17);

打印 true :-)

但是您可以使用 == 比较两个相等的字符串,有时会得到 true 或 false,具体取决于字符串的池化方式...

对,实际上也有类似的情况整数也是如此。

装箱时(将 int 转换为 Integer),编译器对小值 (-128 - 127) 使用缓存,并对相同值重用相同对象,因此可能有点令人惊讶的是,我们有以下内容:

System.out.println(Integer.valueOf(100) == Integer.valueOf(100)); // prints true
System.out.println(Integer.valueOf(200) == Integer.valueOf(200)); // prints false

Yes, when comparing int using == arguments will be unboxed if necessary.

Relevant section from the Java Language Specification:

15.21.1 Numerical Equality Operators == and !=

If the operands of an equality operator are both of numeric type, or one is of numeric type and the other is convertible (§5.1.8) to numeric type, binary numeric promotion is performed on the operands (§5.6.2). If the promoted type of the operands is int or long, then an integer equality test is performed; if the promoted type is float or double, then a floating-point equality test is performed.

Note that binary numeric promotion performs value set conversion (§5.1.13) and unboxing conversion (§5.1.8). Comparison is carried out accurately on floating-point values, no matter what value sets their representing values were drawn from.

Same applies for <, <=, >, >= etc, as well as +, -, * and so on.

So,

System.out.println(Integer.valueOf(17) == 17);

prints true :-)

but you can compare two equal strings with == and sometimes get true or fals depending on how the strings were pooled...

Right, and there is actually a similar situation for Integers as well.

When boxing (transforming int to Integer) the compiler uses a cache for small values (-128 - 127) and reuses the same objects for the same values, so perhaps a bit surprising, we have the following:

System.out.println(Integer.valueOf(100) == Integer.valueOf(100)); // prints true
System.out.println(Integer.valueOf(200) == Integer.valueOf(200)); // prints false
反差帅 2024-12-15 03:47:35

是的,它将拆箱。 第 15.21 节对此进行了介绍JLS .1(数字 == 运算符):

如果等式运算符的操作数都是数字类型,或者一个是数字类型,另一个是可转换的(§5.1.8) 转换为数值类型,对操作数进行二进制数值提升(第 5.6.2 节)。如果操作数的提升类型为 int 或 long,则执行整数相等测试;如果提升的类型是 float 或 double,则执行浮点相等测试。

请注意,二进制数字提升执行值集转换 (§5.1.13) 和拆箱转换 (§5.1.8)。

(我已经链接了第 5.1.8 节,因为这就是关于从 Integerint 的转换可用的内容。)

Yes, it will unbox. This is covered in section 15.21.1 of the JLS (the numeric == operator):

If the operands of an equality operator are both of numeric type, or one is of numeric type and the other is convertible (§5.1.8) to numeric type, binary numeric promotion is performed on the operands (§5.6.2). If the promoted type of the operands is int or long, then an integer equality test is performed; if the promoted type is float or double, then a floating-point equality test is performed.

Note that binary numeric promotion performs value set conversion (§5.1.13) and unboxing conversion (§5.1.8).

(I've linkified section 5.1.8 as that's what talks about the conversion from Integer to int being available.)

迷鸟归林 2024-12-15 03:47:35

这是可能的。

该 Java 功能称为“自动装箱”。

This is possible.

That Java-feature is called Autoboxing.

情感失落者 2024-12-15 03:47:35

是的,它会自动转换。
你也可以做

Integer i = 2;

yes, it's automatically converted.
you can also do

Integer i = 2;
南城旧梦 2024-12-15 03:47:35

是的,这有效,因为自动(取消)装箱。

Yes this works because auto (un)boxing.

走过海棠暮 2024-12-15 03:47:35

它将比较基元 - Integer 将被拆箱。但根据经验:避免这种情况。总是更喜欢基元,并且在将对象与 == 进行比较时要小心,

除了 在 JLS 中看到这一点,您可以通过以下方式验证这一点:

不要使用使用缓存的 Integer.valueOf(2),而是使用新整数(2)。这保证是一个与装箱时 2 所获得的实例不同的实例(装箱发生在 Integer.valueOf(..) 中)。在这种情况下,条件仍然为真,这意味着比较的不是引用。

It will compare primitives - the Integer will be unboxed. But as a rule of thumb: avoid that. Always prefer primitives, and be careful when comparing objects with ==

Apart from seeing this in the JLS, here's how you can verify that:

Instead of Integer.valueOf(2), which uses a cache, use new Integer(2). This is guaranteed to be a different instance than the one that will be obtained if 2 if boxed (the boxing happens with Integer.valueOf(..)). In this case, the condition is still true, which means that it's not references that are compared.

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