Very simply put, because they are two distinct instances of String - two completely seperate Objects in the heap.
When you use the == operator to compare two Objects, you are comparing them by their reference.
In this case the two references point to different Objects, so the result is false.
An Object's hashCode() value is not the same as an Object's reference - it is caluculated in a way specific to the Object's class.
The String class caluculates the hashCode() value based on the internal char array, which is identical for both String instances.
The hashCode() values are therefore identical, even if the Object references are not.
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
S Subbu wrote:Please any one clarrify my doubt . That is how == method work on Objects references. is if two object have same hashcode then i say that two objects are same.
No, that's not true. It's entirely possible to have two different Objects with the same hash code. hashCode() returns an int. Think about it - how many different int values are there? It's not hard to have more objects than that.
== returns true if and only if the references are to exactly the same Object.
1. The purpose of the hashcode is to assist in putting objects into hash-based collections (i.e. Maps).
2. Java checks whether two references point at the same object by comparing the references to see if they contain the same value.
(By the way #2 is pretty much like asking how Java checks whether two integer values are the same. And also by the way, asking how Java does something is basically the wrong way to learn things. You should learn what it does.)