@RichardJPLeGuen I saw the edit, and rolled back but then applied some of your changes. The thing is, I wasn't knowledgeable enough to ask the question in the same way that you did, so someone without your knowledge (keywords, phrasing, etc) might not be able to find the question using simpler terminology.
–
trusktrOct 5 '12 at 23:03

5 Answers
5

Very simply the arithmetic comparison operators == and != compare the object references, or memory addresses of the objects. >, and < and related operators can't be used with objects.

So ==, != is useful only if you want to determine whether two different variables point to the same object.

As an example, this is useful in an event handler: if you have one event handler tied to e.g. multiple buttons, you'll need to determine in the handler which button has been pressed. In this case, you can use ==.

Object comparison of the type that you're asking about is captured using methods like .equals, or special purpose methods like String.compareTo.

It's worth noting that the default Object.equals method is equivalent to ==: it compares object references; this is covered in the docs. Most classes built into Java override equals with their own implementation: for example, String overrides equals to compare the characters one at a time. To get a more specific/useful implementation of .equals for your own objects, you'll need to override .equals with a more specific implementation.

So what does .equals() compare? Does it look to see if both objects have the same methods and properties and whether the properties are also equal? Or does it just check if the properties are equal? Or does it compare them bit to bit?
–
trusktrOct 5 '12 at 22:50

Look at the implementation of .equals in string. it's a good start. typically, you first check for reference equality (==), then assignment compatibility (usually with the instanceof operator), and then finally, object specific data comparison. In the case of string, it's the underlying character data.
–
MattOct 6 '12 at 2:21

No it doesn't. It compares whether the two variables are references to the same objects.

Unless you're dealing with types which are subject to autoboxing, such as Integer, you can't use > and < with objects at all.

In the case where you are using an autoboxed type, java doesn't look for specific methods, but will auto-unbox the variables, turning them into primitives - but this isn't the case for the equals operator. The == operator will always compare objects as references, even when comparing autoboxed objects:

You didn't try it yourself, apparently, because <, >, <= and >= do not work on Objects.

However, == compares the left and right operand. When they are binary the same, it results in true. In the case of objects, in compares the pointers. So which means that this will only result in true if the Object is left and right the very same object in memory.

Other methods, like compareTo and equals are made to provide a custom method of comparing to different objects in memory, but which might be equal to each other (i.e. the data is the same).

In case of Strings, for example:

String str0 = new String("foo");
String str1 = new String("foo");
// A human being would say that the two strings are equal, which is true
// But it are TWO different objects in memory. So, using == will result
// in false
System.out.println(str0 == str1); // false
// But if we want to check the content of the string, we can use the equals method,
// because that method compares character by character of the two objects
String.out.println(str0.equals(str1)); // true
String.out.println(str1.equals(str0)); // true

I wouldn't love it. It would be a huge pain in the ass... How would you ever check if two variables point to the same place in memory, which means that it is only one single object.
–
Martijn CourteauxOct 5 '12 at 22:40

@MartijnCourteaux Maybe something like if (x.is(y)) where .is() would be inherited from Object for all objects. I would prefer if x==y used compareTo to return true or false.
–
trusktrOct 5 '12 at 22:47