Reputation: 4362
Consider the following conditions:
if (isCustom() == new Long(1)) {
System.out.println("ok 1");
}
if (1L == new Long(1)) {
System.out.println("ok 2");
}
For the following definition:
static Long isCustom() {
return 1L;
}
Output is:
ok 2
But for
static long isCustom() {
return 1L;
}
Output is:
ok 1
ok 2
I am not getting the point here!
Upvotes: 0
Views: 93
Reputation: 1115
This is all to do with java primitives and boxed types. The primitive version long
gets automatically converted by java ("boxed") to the object version - Long
, and vice versa for "unboxing".
In the first scenario above, you're returning a boxed primitive Long
(i.e. an object), and comparing to see if the object has the same reference as a new object you've just created. In other words you're comparing a Long
with another Long
. Since you created a new Long()
, the objects are different, so the if statement returns false and "ok 1" is not printed.
In the second scenario, you're returning the primitive type, and comparing a primitive with an object - i.e. a long
with a Long
. In this case, the object (Long
) gets "unboxed" to a primitive type, and when comparing two primitive types with ==
, you're not checking the object reference but rather you're checking to see if they have the same value.
The second case ("ok 2") is always comparing a primitive long
with an object Long
, and so is always true for the same reasons as the second scenario above.
Upvotes: 1
Reputation: 178263
In the first example, your isCustom()
method is returning a Long
-- the 1L
is boxed to a Long
object. When you use the ==
operator on it, Java uses reference equality. Those two Long
objects, even though they represent the same long
-- 1L
-- are different objects, so the condition is false
and "ok 1"
is not printed. Note that certain Long
s are not required to be cached like certain Integer
s.
Note that unlike the corresponding method in the
Integer
class, this method is not required to cache values within a particular range.
In the second example, your isCustom()
method is returning a long
-- no boxing is performed here. The ==
operator then unboxes the Long(1)
to 1L
to compare it to the long
returned by isCustom()
, the condition is true
, and "ok 1"
is printed.
Upvotes: 2
Reputation: 59112
In the first case you have two Long
objects, and you are checking if they are the same object (because you are using ==
, not .equals
). They are not the same object.
In the second case you have a long
value being compared to a Long
object, and the Long
gets unboxed into a long
value. ==
works fine for long
values.
Upvotes: 3
Reputation: 20396
In the former case, you're returning an Object that was autoboxed.
In the latter case, you're returning a long value.
Because the latter is just a value, the code is evaluating equivalency of the values 1L and [the autounboxed value of an object with the value] 1L.
In the former case, the code is evaluating the equivalency of the identities of two objects that both have the value 1L.
Upvotes: 4