Samarland
Samarland

Reputation: 581

with longValue Using equals to compare two reference

I was using the != to compare theses two fields, But I discovered I have to use the .equals to compare them.

    categoryId.longValue() != PDTO.STOREID

&& categoryId.longValue() != mDTO.getCategoryId()){

I edited my code like this, But It doesn't help me,(Cannot invoke equals(long) on the primitive type long)

    categoryId.longValue().equals(PDTO.STOREID && categoryId.longValue().equals(mDTO.getCategoryId()){

Any help?

Upvotes: 1

Views: 240

Answers (2)

JB Nizet
JB Nizet

Reputation: 691943

Primitives are compared with ==. Objects are compared with equals() (otherwise you check that both variables point to the exact same object). So, either you use

a.longValue() == b.longValue()

or you use

a.equals(b);

Both are equivalent (except the first one will throw an exception id b is null).

Upvotes: 1

Mureinik
Mureinik

Reputation: 311823

equals is a method, it should be invoked on an object - in your case, an instance of java.lang.Long. Just drop the longValue() and you should be OK:

categoryId.equals(PDTO.STOREID) && categoryId.equals(mDTO.getCategoryId())

Upvotes: 1

Related Questions