开发者

Integer == int allowed in java

开发者 https://www.devze.com 2023-04-11 17:11 出处:网络
I was wondering if java automatically turns a Integer into an int when comparing to an int? Or will the == try and compare references o开发者_开发问答n primitives?

I was wondering if java automatically turns a Integer into an int when comparing to an int? Or will the == try and compare references o开发者_开发问答n primitives?

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

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


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


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.)


This is possible.

That Java-feature is called Autoboxing.


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

Integer i = 2;


Yes this works because auto (un)boxing.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号