Discussion in 'OT Technology' started by Yoshiemaster, May 1, 2006.
explain the differene between "==", and ".equals"....bitches.
== is an operator that's not overloaded for the String class.
String.equals() is a member function of the String class that does a character by character comparison of the parent String object and the argument to equals().
The equals() method determines whether two strings have the same characters, whereas the == operator determines if two operands refer to the same String object. == actually tests to see if the references in the variables point to the same memory address.
thanks, that was a good answer.
you cant test if two objects are equal using ==, you would need to use .equals method, this applies to all objects not just strings. If you are writing your own object, you would have to write your own equals method or implement comparable.
== is also used for base types, like int, double, char, etc.
.equal is used for everything else
Some of the above answers aren't actually correct.
== tests for reference equality. It will return true if the variables it tests refer to the exact same instance of an object (or primitive).
The equals method tests for whether two instances of an object are equal. What properties of the object that make them equal is left for the user implementing the class to decide.
Point a = new Point(1,2);
Point b = new Point(1,2);
a == b will return false.
a.equals(b) will return true.
In what way is that different from the answers we posted?
oh your answer is correct. i was just generalizing...it just seems like a lot of people don't actually realize that they often need to override the equals method for any objects you write yourself for it to work appropriately (at least as defined by the Object contract). Even less seem to realize that overriding equals means you are also required to override hashcode.