Compare two primitive long variables in java

The name is pretty clear. I am moving from C # to Java. I have an object and a getter method that returns its identifier. I want to compare the identifiers of two objects of the same type and check if their identifiers are equal.

tried:

obj.getId() == obj1.getId(); 

 Long id1 = obj.getId(); Long id2 = obj1.getId(); assertTrue(id1.equals(id2)) 

 assertTrue(id1== id2) 
+8
java long-integer compare
source share
6 answers

In java:

  • The == operator tells you whether both operands are the same object (instance).
  • The .equals() method on Long tells you if they are equal in value.

But you should not do that either. The correct way to do this is:

 assertEquals(id1, id2); 

With assertEquals() , if the assertion fails, the error message will tell you what are the two values, for example expected 2, but was 5 , etc.

+21
source share

Try the following:

 assertTrue(id1.longValue() == id2.longValue()) 
+2
source share

They should not be equal using ==. Use either:

 id1.equals(id2) 

or

 id1.longValue() == id2.longValue() 

In both cases, I left zero checks, because I doubt that you will forget them if you need them here;)

So, why is Long not always == another Long with the same value? The answer is simple. They are different objects! Depending on how Long was received, they may be the same due to internal caching. See Long.valueOf .

+2
source share

In addition to all comments on proper comparisons, strong typing can help you solve your problem.

Does getID () return something of type Long (class type) or type long (which is a primitive type)? The problem is that if the method returns a primitive value, then comparing with '==' and 'equals () will work, because Java automatically brings primitives to wrapper objects when you need them.

But otherwise, if getID () returns Long, the comparison with "==" will most likely fail because it checks to see if you have the same object, and not if the objects have the same value.

+2
source share

You need to know the difference between Long and Long - Long is a primitive type, Long is a shell type. (A bit like a box value in C #, but strictly typed.) What type of getId() return?

Just:

 assertEqual(id1, id2); 

It should be good if you do this in a test. Otherwise, you can use:

 if (id1.equals(ids2)) 

if they are definitely not null, or use Guava :

 if (Objects.equal(id1, id2)) 

to handle invalidation. (You can write Objects.equal yourself, of course, but you definitely need to get hold of Guava, so you could also use this ..)

It is worth noting that some wrapper objects are reused - for example, for example:

 // This will work Long x = 5L; Long y = 5L; assertTrue(x == y); // Reference comparison // This *probably* won't but it could! x = 10000L; 

y = 10000L; assertTrue (x == y); // Compare links

+1
source share

in java,

use == to compare primitives and x.equals(y) to compare objects.

I'm not sure what your question is ... it looks like you tried both and didn't work?

Note: you use Long instead of Long , so you want to use .equals()

( Long is primitive, Long is an object)

+1
source share

All Articles