When using == for a primitive and a boxed value, is autoboxing done, or is unboxing done

前端 未结 3 1991
野趣味
野趣味 2020-11-27 04:05

The following code compiles (with Java 8):

Integer i1 = 1000;
int i2 = 1000;
boolean compared = (i1 == i2);

But what does it do?

Un

3条回答
  •  迷失自我
    2020-11-27 04:23

    Explanation

    1. When two primitive values are compared using == operator autoboxing does not take place.

    2. When two objects are compared using == operator autoboxing plays role.

    3. When mixed combination is used that is it contains an Object and primitive type and comparison is done using == operator unboxing happens on the Object and is converted to primitive type.

    Please go through the below link which will help you get understand detailed about auto-boxing with suitable example.

    Refer Link : http://javarevisited.blogspot.in/2012/07/auto-boxing-and-unboxing-in-java-be.html

提交回复
热议问题