Java Strings

12 Jun

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.

The following example will simplify my point.

public class MWC109 {
public static void main(String args[]) {
String a = "A", b = "B", c = a+b, d = a+b;
System.out.println("a+b hashcode: " + (a+b).hashCode());
System.out.println("c hashcode: " +  c.hashCode() );
System.out.println("d hashcode: " + d.hashCode());
System.out.println("c system hash code: " + System.identityHashCode(c) );
System.out.println("d system hash code: " + System.identityHashCode(d) );
for (int i =0; i<10; i++){
System.out.println(i + ") a+b System HashCode: " + System.identityHashCode((a+b)));
String str1 = "POps";
String str2 = "POps";
System.out.println("str1 System HashCode: " +  System.identityHashCode(str1));
System.out.println("str2 System HashCode: " + System.identityHashCode(str2));
System.out.println("str1 hashcode: " + str1.hashCode());
System.out.println("str2 hashcode: " + str2.hashCode());
for (int i=0; i<10; i++){
System.out.println(i + ") str1+str2 System HashCode: " + System.identityHashCode(str1+str2));
System.out.print(((a+b)==(a+b)) + ",");
System.out.print((c==d) + ",");

The output of the above code is

a+b hashcode: 2081
c hashcode: 2081
d hashcode: 2081
c system hash code: 4072869
d system hash code: 1671711
0) a+b System HashCode: 11394033
1) a+b System HashCode: 4384790
2) a+b System HashCode: 9634993
3) a+b System HashCode: 1641745
4) a+b System HashCode: 11077203
5) a+b System HashCode: 14576877
6) a+b System HashCode: 12677476
7) a+b System HashCode: 33263331
8) a+b System HashCode: 6413875
9) a+b System HashCode: 21174459
str1 System HashCode: 827574
str2 System HashCode: 827574
str1 hashcode: 2462786
str2 hashcode: 2462786
0) str1+str2 System HashCode: 17510567
1) str1+str2 System HashCode: 27744459
2) str1+str2 System HashCode: 28737396
3) str1+str2 System HashCode: 6927154
4) str1+str2 System HashCode: 24355087
5) str1+str2 System HashCode: 5442986
6) str1+str2 System HashCode: 10891203
7) str1+str2 System HashCode: 9023134
8) str1+str2 System HashCode: 19336051
9) str1+str2 System HashCode: 6336176

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.

public int hashCode() {
int h = hash;
if (h == 0) {
int off = offset;
char val[] = value;
int len = count;
for (int i = 0; i < len; i++) {
h = 31*h + val[off++];
hash = h;
return h;

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.v

1 Comment

Posted by on June 12, 2009 in Java



One response to “Java Strings

  1. Teatastency

    June 19, 2009 at 3:08 am

    Thank you for blog post. It’s very imformative stuff.
    I enjoy to browse


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: