You need to be aware of the difference between Long
and long
- long
is the primitive type, Long
is the wrapper type. (A bit like a boxed value in C#, but strongly typed.) What's the return type of getId()
?
Simply:
assertEqual(id1, id2);
should be fine if you're doing this in a test. Otherwise, you could use:
if (id1.equals(ids2))
if they're definitely not null, or use Guava:
if (Objects.equal(id1, id2))
to handle nullity. (You can write Objects.equal
yourself, of course, but you should definitely get hold of Guava anyway, so you might as well use that...)
It's worth noting that certain wrapper objects are reused - so 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); // Reference comparison