13

The title is pretty self-explanatory. I'm moving from C# to Java. I have an object and a getter method which returns its ID. I want to compare the ids of two objects of the same type and check if the values of their ids are equal.

tried:

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

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

assertTrue(id1.equals(id2))

assertTrue(id1== id2)
Bohemian
  • 412,405
  • 93
  • 575
  • 722
Dragan
  • 3,713
  • 12
  • 42
  • 59
  • Actually this question is confusing because it is not about comparing, but about testing for equality. See http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html#compareTo(T) – Adriaan Koster Mar 09 '15 at 09:03
  • 3
    you are not trying to compare two primitive longs You are trying to compare two boxed Long. – Jiechao Wang Mar 20 '19 at 17:20

7 Answers7

23

In java:

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

But you shouldn't do either. The correct way to do it is this:

assertEquals(id1, id2);

With assertEquals(), if the assertion fails, the error message will tell you what the two values were, eg expected 2, but was 5 etc

Bohemian
  • 412,405
  • 93
  • 575
  • 722
6

To compare two primitive long you can simply use ==

Example:

long x = 1L;
long y = 1L;

if (x == y) {
 System.out.println("value of x and y are same");
}

To compare two Long objects you can use Long.compare(long x, long y). This method was added in java 1.7. Below is the method implementation:

public static int compare(long x, long y) {
        return (x < y) ? -1 : ((x == y) ? 0 : 1);
}

Example:

Long x = new Long(1);
Long y = new Long(1);
if (Long.compare(x,y) == 0) {
  System.out.println(values of x and y are same);
}
Another_Dev
  • 86
  • 1
  • 5
3

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 did neither work?

Note, you are using Long instead of long, so you want to use .equals()

(long is a primitive, Long is an object)

edthethird
  • 6,263
  • 2
  • 24
  • 34
  • Thank you! I was comparing the modified dates of two files that should have been the same (because one was set to the other,) but == was always failing, even though when I set breakpoints and checked them, they were identical. Turns out my mistake was the capital L in "Long jFileTime = jFile.lastModified();"... replaced it with a lowercase l and suddenly my == comparison started working. Really annoying that it doesn't give any compiler warning about how I have an implied, non-explicit cast occurring... – ArtOfWarfare Sep 26 '12 at 18:29
2

Try doing the following:

assertTrue(id1.longValue() == id2.longValue())
bsimic
  • 926
  • 7
  • 15
2

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

id1.equals(id2)

or

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

In both cases I left out null-checks because I doubt you'd forget them, if they were needed here ;)

So, why is Long not always == another Long with the same value? The answer is easy. They're different objects! Depending on how the Long was retrieved they might be the same, because of internal caching. See Long.valueOf.

spidey
  • 238
  • 2
  • 7
2

In addition to all the comments about the right comparison, strong typing can help you resolve your issue.

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

But the other way, if getID() returns a Long value, comparing with '==' is likely to fail, because that checks if you have the same object, not if the objects have the same value.

Jochen
  • 2,277
  • 15
  • 22
1

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
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • The assertTrue statement won't work with larger values above 127. https://stackoverflow.com/questions/20541636/comparing-boxed-long-values-127-and-128 – Lyle Z Nov 08 '18 at 00:11
  • @LyleZ: Won't is too strong an assertion. It might, it just isn't guaranteed to. – Jon Skeet Nov 08 '18 at 07:10