Despite the comments above, I suspect that you already understand that ==
determines if two references point to the same object (or are both null), and that you should use equals()
if you want to compare two strings for data-equality.
Rather, I think what you're missing is that the hashCode()
method corresponds to the equals()
method in this respect; it's based on the data in an object, and in fact, it's specified that classes should always implement hashCode()
in such a way that if a.equals(b)
, then a.hashCode() == b.hashCode()
. (Of course, there's nothing in the language that enforces this.) The analogue of ==
that you're looking for is the System.identityHashCode() method.
However, even there it should be noted that System.identityHashCode()
does not guarantee that distinct instances will have distinct identity hash codes. (It can't, because it's possible to have more than 232 objects in a JVM at the same time . . . granted, not all JVMs support that; but nothing in the Java language specification forbids it.)