You should compare Strings (and Objects in general) using 'equals', not using '=='. equals tests equality of the object values, while == tests equality of object references. Even for non-mutable, seemingly identical, objects like the ones in your example, the references may or may not be the same.
Two distinct objects that are unequal (according to the Object.equals(Object) method) may produce the same hash codes (according to the Object.hashCode() method). In fact, unless construction is controlled (which best practices mandates anyway, but common practice ensure that it is prolific), it's impossible to enforce - all you need to do is construct 2^32 + 1 unequal instances and a hash code collision is guaranteed.
This may be all beside the point, since your question is unclear, in which case, ignore it.
One more fact is in String hashCode() is overridden & it do not represent memory address but hashCode depends on String Contents.string (2 different objects ) can have same hashcode provided that their contents are same.
One more fact is in String hashCode() is overridden & it do not represent memory address but hashCode depends on String Contents.string (2 different objects ) can have same hashcode provided that their contents are same.
Indeed. Objects that are equal *should* generate the same hash code.
Originally posted by Shrinivas Mujumdar: string (2 different objects ) can have same hashcode provided that their contents are same.
They *must* have the same hashcode if they are equals, and they *may* have the same if they are not equal.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus