
Signature
Chris "isn't 'autoboxing' a video game?" Dollin
Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN
Thanks for your reply Chris,
if not autoboxing is happening then the following code should output
true
1 int i = 5;
2 Integer ii = new Integer(5);
3 System.out.println(i == ii);
which is does :)
Im still not completely clear on how == can be used with primitives.
My understanding is that == checks for object equality (the bit
pattern of the object). Does anyone know how == works with primitives?
TIA
Richard
Andreas Leitgeb - 20 Nov 2007 12:42 GMT
> Im still not completely clear on how == can be used with primitives.
Which is funny, because a lot of java-newbies find it just natural
how primitives are handled, and have problems understanding "=="
for the object-wrappers.
> My understanding is that == checks for object equality (the bit
> pattern of the object). Does anyone know how == works with primitives?
"==" always compares primitive types: either it compares values
(int,long,float,double,byte,short,...), or it compares references,
but it never compares objects!
To compare Objects, you'd use Object's .equals(other) method, as
overloaded appropriately for any class that you actually use.
PS: in (Integer == int)-type comparisons, Seemingly it unboxes
the object, rather than box the primitive.
You can verify this, by using javap (or whatever other
class-file-disassembler) on your sample.class and see what it
really does. ... it calls Integer's intValue() method, and
does an int-compare. (if_icmpne)
class A {
public static void main(String[]args){
Integer i1=new Integer(42);
int i2=42;
System.out.println( (i1 == i2) );
}
}
Chris Dollin - 20 Nov 2007 13:42 GMT
> Thanks for your reply Chris,
>
[quoted text clipped - 6 lines]
>
> which is does :)
That's not because of autoboxing; that's because of auto/un/boxing
in the special treatment of `==`.
> Im still not completely clear on how == can be used with primitives.
By comparing their values.
> My understanding is that == checks for object equality
No: it compares values, not objects. That is, if its operands are
primitive values, it compares those values; if they are references
(ie a value of class/interface type) then it compares those references;
and if they're mixed, it may apply autounboxing.
> (the bit pattern of the object).
It may come down to that, yes.
> Does anyone know how == works with primitives?
In what sense? It compares their values. How else could it work?

Signature
Chris "rhetorical" Dollin
Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England
Mark Space - 20 Nov 2007 20:40 GMT
> Thanks for your reply Chris,
>
[quoted text clipped - 10 lines]
> My understanding is that == checks for object equality (the bit
> pattern of the object). Does anyone know how == works with primitives?
Maybe this was mentioned somewhere, though I didn't see it.
Reading line 3, I start with an int i, then see I ==, then I see an
Integer. If I were a parser, I'd be temped to convert the Integer to
the type of the existing expression (int in this case) first. Since I
can do that, I'd unbox the Integer to an int and continue. Hence, ==
will compare two int's and gives true as a result.
I'm not sure, but that's what I speculate.
Try switching i and ii above and see if you get a different result...
Patricia Shanahan - 20 Nov 2007 21:14 GMT
>> Thanks for your reply Chris,
>>
[quoted text clipped - 20 lines]
>
> I'm not sure, but that's what I speculate.
Why speculate, when the JLS tells all? Well, at least quite a lot,
including the answer to this question.
"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)."
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.21.1
Patricia