Question

I couldnt use equals with long values:

    long a = 20; //or 20L
    long b = 20; //or 20L
    if(a.equals(b)) {
        //do
    }

error: Cannot invoke equals(long) on the primitive type long

Also == does not work with long values.

How can I check if long values equals?

Was it helpful?

Solution

== does work with long values.

OTHER TIPS

== operator used to compare primitive value and equals() method used to compare Object

Since, long is a primitive type then you have to use == operator for primitive type data as below...

if(a == b ) {
    //do
}

The equals() is used to compare 2 Object and Long is an Object type of long. If you declare your instances as Long type then you can use equals() method as below...

Long a = 20;
Long b = 20;

if(a.equals(b)) {
    //do
}

For primitive data types you should use ==.

And for objects you should use the equals function.

In your case it would be:

long a = 20; //or 20L
long b = 20; //or 20L
if(a == b) {
    //do
}

== works for long, if you want to use equals:

Long a = new Long(20);
Long b = new Long(20);
if(a.equals(b)) 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top