Java new long (0)

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 introduce a condition, since both are equal to zero, but it goes into the else condition. I even tried to debug the code:

label_id    Long  (id=142)  
    value 0

Did I miss something?

+4
source share
5 answers

You must first extract the value label_idand then compare it:

if(item.label_id.longValue() == 0L)
+3
source

You are using object comparison. and with the new ... you create a new object. both objects do not match ... you can use the new Long (0) .equals (...)

+6
source

== Java . new Long(0) , , Long.

equals, NullPointerExceptions`.

if(item.label_id.equals(new Long(0))) {

} else {

}

,

if(item.label_id == Long.valueOf(0)) {

} else {

}
+1

@Rocket @ParkerHalo, , new Long(0) .

, if , new Long(0) Long, , , . , ( ). C, ( ) Java. Long , longs .

, new Long(0) , .

- , @ParkerHalo, , long Zero. , , Zero, :

if(item.label_id.longValue() == 0L) {

0 , L longs.

, !

+1

item.label_id.longValue() == new Long(0).longValue()

item.label_id.equals(new Long(0))

/

-2

Source: https://habr.com/ru/post/1615587/


All Articles