Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java new long(0)

Tags:

java

In my class I have this parameter:

public Long label_id ;

When I try to evaluate the value from label_id which is 0

if(item.label_id == new Long(0)) {
    Doesn't enter here
} else {
    Enters here
}

It is supposed to enter the condition since both are zero but it enters the else condition. I even tried debugging the code:

label_id    Long  (id=142)  
    value 0

Am I missing something here?

like image 796
Arnold Cristobal Avatar asked Nov 12 '15 14:11

Arnold Cristobal


2 Answers

You should extract the value of label_id first and then compare it:

if(item.label_id.longValue() == 0L)
like image 64
ParkerHalo Avatar answered Nov 18 '22 03:11

ParkerHalo


You are using an object comparison. and with new ... you generate a new object. the both objects are not the same... You can use new Long(0).equals(...)

like image 6
Rocket Avatar answered Nov 18 '22 02:11

Rocket