1

I suppose that conversions described in jls are sorted according the priority. first has greate priority.

jls

Thus I solved that Boxing has greater priority than Unboxing. I decided to check this assumption.

research following code:

public class BoxingUnboxingPriority {
    public static void main(String [] args){
        int sn = 1000;
        Integer isn1= new Integer(sn);
        System.out.println(sn == isn1 );

    }
}

out:

true

What is boxing? is just new Integer(primitiveInt)

I changed the code a bit

int sn = 1000;
Integer isn1= new Integer(sn);
Integer isn2= new Integer(sn);
System.out.println(isn1 == isn2 );

out:

false

Thus I made mistake.

Please clarify me this issue.

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710

4 Answers4

2

The relevant section:

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

So:

Integer o = 1;
int i = 1;
boolean b = o == i;

...is equivalent to:

boolean b = o.intValue() == i;

Where both are of type Integer neither is a primitive numeric type - they are both object references.

Community
  • 1
  • 1
McDowell
  • 107,573
  • 31
  • 204
  • 267
1

When you use primitive with a Wrapper object, that wrapper object will be unboxed and then the operation will be applied.

In your first case, when you comparing sn with isn1, isn1 will be unboxed and the value will be compared. So you got true.

In second case, isn1, isn2 are two different object, so == operator will give false

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
0

I suppose that conversions described in jls are sorted according the priority.

That is incorrect. The JLS does not talk about "priorities" for conversions. It is not a recognized concept.

In fact, the conversions that can be applied are documented on a case by case basis for each operator, and so on. Thus JLS 15.21.1 says that == or != for numeric types results in "binary numeric promotion" of both operands. And JLS 5.6.2 says that binary numeric promotion consists of "unboxing conversion" (5.1.8) followed by "widening primitive conversion" (5.1.2) and finally "value set conversion" (5.1.3).

By contrast, JLS 15.21.3 says that when two references are compared using == or !=, no promotions or conversions take place.

(In fact, a common Java beginners mistake is to use == to compare two Integer objects rather than the equals(Object) method. And probably that is what "the question" you are looking at is trying to test your understanding of ...)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

int sn=1000; Integer isn1=new Integer(sn); System.out.println(sn == isn1 );

will be converted to

System.out.println(sn == isn1.intValue());

while comparing primitive with wrapper the wrapper object first will be unboxed and then comparison. wrapper object intValue() returns int so due to primitive comparison result true.

Integer isn1= new Integer(sn);

Integer isn2= new Integer(sn); System.out.println(isn1 == isn2 );

// comparing two different object so false.

Vikas
  • 490
  • 4
  • 10