I have a method that returns a Long object datatype via invocation of: resp.getResultCode()
. I want to compare it HttpStatus.GONE.value()
which actually just returns a primitive int value of 410
. Would the Long unbox itself to properly compare with the int primitive?
if(resp.getResultCode() == HttpStatus.GONE.value()){
// code inside..
}
Here's the JLS explanation
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).
and
If the promoted type of the operands is
int
orlong
, then an integer equality test is performed.
So the Long
is unboxed to long
. And numeric promotion is applied to int
to make it a long
. Then they are compared.
Consider that case where long
would be "demoted" to an int
, you'd have cases like this
public static void main(String[] args) throws Exception {
long lvalue = 1234567891011L;
int ivalue = 1912277059;
System.out.println(lvalue == ivalue); // false
System.out.println((int) lvalue == ivalue); // true, but shouldn't
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.