Originally posted by Brian Lugo:
Why this anomaly?
What's strange about that? The code does exactly what I would expect. The '==' operator, when used on object references, only compares the references to determine if they reference the same object. Look here:
The references contained in o and q are the same (because of the assignment operator). That means that they both reference the same object and the '==' operator will return true. p, however, references a different object. Therefore, comparing that reference with o or q, we get a result of false, indicating that the two references do not reference the same object.
Now, when dealing with the equals() method, the Object class provides an implementation that does the same thing as the '==' operator. However, some class override this method to check for a different type of equality. The String class and the wrapper classes do just that. They each define their own type of equality. For Strings, they are considered equal if they contain the same list of characters (case sensitive). For the wrapper classes, they check the internal primitive values to determine if they are equal.
Does that help clear up the difference between '==' and equals()?
Corey