R
richnjones
Hello all,
Im a little confused on how number comparison is done. Consider
1 Integer i = new Integer(5);
2 Integer ii = new Integer(5);
3 Long l = new Long(5);
4
5 System.out.println("two Integers " + (i == ii));
6 System.out.println("Integers and Long.intValue " + (i ==
l.intValue()));
The output of this is
two Integers false
Integers and Long.intValue true
The first false I can understand. Using new will create new objects so
that the == will fail. (Correct me if I am wrong). The second output I
expected to be false.
This is how I see things...
1 I have an integer object and a long object
2 Doing l.intValue() will return an int which will be autoboxed up to
an Integer
3 This new Integer I thought would be a new object and so the ==
should fail.
Is the autoboxing being clever and seeing that there is another object
with the same value and just assigning the reference to that? (Like
with Strings)
Why is the second output not false?
TIA
Richard
Im a little confused on how number comparison is done. Consider
1 Integer i = new Integer(5);
2 Integer ii = new Integer(5);
3 Long l = new Long(5);
4
5 System.out.println("two Integers " + (i == ii));
6 System.out.println("Integers and Long.intValue " + (i ==
l.intValue()));
The output of this is
two Integers false
Integers and Long.intValue true
The first false I can understand. Using new will create new objects so
that the == will fail. (Correct me if I am wrong). The second output I
expected to be false.
This is how I see things...
1 I have an integer object and a long object
2 Doing l.intValue() will return an int which will be autoboxed up to
an Integer
3 This new Integer I thought would be a new object and so the ==
should fail.
Is the autoboxing being clever and seeing that there is another object
with the same value and just assigning the reference to that? (Like
with Strings)
Why is the second output not false?
TIA
Richard