Integer == int allowed in java

后端 未结 6 989
自闭症患者
自闭症患者 2020-12-06 07:29

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 t

相关标签:
6条回答
  • 2020-12-06 08:12

    Yes this works because auto (un)boxing.

    0 讨论(0)
  • 2020-12-06 08:14

    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
    
    0 讨论(0)
  • 2020-12-06 08:15

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

    0 讨论(0)
  • 2020-12-06 08:26

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

    Integer i = 2;
    
    0 讨论(0)
  • 2020-12-06 08:29

    This is possible.

    That Java-feature is called Autoboxing.

    0 讨论(0)
  • 2020-12-06 08:31

    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 讨论(0)
提交回复
热议问题