I am not sure to what extent i am right. But i think i will share it. when we say, c=a+b, it will call, concat() of string class or append() of StringBuffer class(internally it would look like this:- new StringBuilder().append(a).append(b).toString(); )
( not sure which one of these)
Where in both of them returns a new string.
[ December 11, 2008: Message edited by: James Tharakan ] [ December 11, 2008: Message edited by: James Tharakan ]
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Whenever we make a String object by doing some operation on the Strings then we will always end up with new String Object.
String class has overridden hashCode() and equals() method so meaningfully the String objects may be equal and they will return the same hash code but then also they are situated at different memory location in the Objcet heap.
The default implementation of hashCode() provided by Object is derived by mapping the memory address of the object to an integer value. Because on some architectures the address space is larger than the range of values for int, it is possible that two distinct objects could have the same hashCode(). If you override hashCode(), you can still use the System.identityHashCode() method to access this default value. src
The following example which I prepared by altering the above question will simplify my point.
The output of the above code is
So it is visible that whenever we make some String object by doing some alteration we will always end up in a new Object in heap. So the system HashCode will be different for them as no two Objects can share the same memory.
The hashcodes we get from the String Objects return the hashcode generated by this function as it is overridden in String class.
To get the actaul hashcode we used the static function identityHashCode() of System class.
The system hashcode for str1 and str2 are same as those objects were created without any alteration on any of the objects.