Bimal Bose B S
Bimal Bose B S

Reputation: 231

Conditional with number1 == number2 (of type Long) is never true?

this code is supposed to list recent calls with recent same nos skipped but they are being displayed, please help

             //code
              Long number0=(long) 0;   
          // loop through cursor 
          while(mCallCursor.moveToNext()){
              Long number1 = mCallCursor.getLong(0);
              if(number1==number0)
                  continue;
              else
                  number0=number1;
              if(mCallCursor.getString(2)!=null){
                  String name = mCallCursor.getString(2);
                  System.out.println(name);
              }
              else
                  System.out.println(number1);

          }

Upvotes: 2

Views: 209

Answers (2)

hevi
hevi

Reputation: 2726

The main reason why it is not working is that Long 's are objects and == operator works as it is testing equalities of two objects not the long values stored in these objects. On the other hand a long is not an object but a primitive.

if((long)number2 == (long)number1)

shall work as well.

Upvotes: 0

Ted Hopp
Ted Hopp

Reputation: 234847

Instead of

if(number1==number0)

use

if(number1.equals(number0))

Two Long values can satisfy equals without being ==.

Upvotes: 5

Related Questions