0

this will be a simple question (or at least i think). I have to compare a Long object to a long primitive.

My code simplified:

long primitiveLong = 285825645621;
Long objectLong = new Long(285825645621);

if(primitiveLong == objectLong.longValue()){
    System.out.print("They are equals");
}

I expected that it would have worked, but it didn't, can someone explain me why?

Thanks in advance.

EDIT

Ok, i did a mistake using explicit numbers, i'll put here my code:

long identifier = mMarkers.get(marker);
long currentId = item.getId().longValue();

if (currentId==identifier)
    System.out.print("They are equals");

In debug

enter image description here

EDIT 2

It was an IDE (Android Studio) bug in debug mode, i tried on another IDE and it worked well with same code. So thanks to everyone for the answers, but the problem was another, that i couldn't expect.

Giorgio Antonioli
  • 15,771
  • 10
  • 45
  • 70

2 Answers2

0

First this must should give you compile error because compiler think of 285825645621 as an Integer but it is larger than int, so you need to change your code like this:

long primitiveLong = 285825645621l; //add and l at the end of your number so compiler know that it is a long not an int
Long objectLong = new Long(285825645621l); //do the same thing here

if(primitiveLong == objectLong.longValue()){
    System.out.print("They are equals");
}

and this will print : They are equals

Lrrr
  • 4,755
  • 5
  • 41
  • 63
0

You can use your literal value still this limit 285825645

If you are using beyond the limit you will get compile error 

long primitiveLong = c;
            Long objectLong = new Long(285825645);

            if(primitiveLong == objectLong.longValue()){
                System.out.print("They are equals");
            }