Object references (III)

3 March 2008

This post is in continuation of Object references (I) anf (II). Please read those before this one.

Just to make thing more obvious, let me give a very interesting example:

Student obj1 = new Student(1, "Laiq");
Student obj2 = new Student(2,"Dave");
 
System.out.println("HashCode obj1: " + obj1.hashCode());
System.out.println("HashCode obj2: " + obj2.hashCode());
 
obj1 = obj2;
 
System.out.println("HashCode obj1: " + obj1.hashCode());
System.out.println("HashCode obj2: " + obj2.hashCode());

Output:

HashCode obj1: 26022015
HashCode obj2: 3541984
HashCode obj1: 3541984
HashCode obj2: 3541984

Its clear that obj1 and obj2 were two different objects with different set of data. But, when we assigned obj2 to obj1 using equal to operator, both started referring to the same set of data (object in memory).

I hope this will clear your concepts.

Happy coding.

del.icio.us:Object references (III)  digg:Object references (III)  spurl:Object references (III)  wists:Object references (III)  simpy:Object references (III)  newsvine:Object references (III)  blinklist:Object references (III)  furl:Object references (III)  reddit:Object references (III)  fark:Object references (III)  blogmarks:Object references (III)  Y!:Object references (III)  smarking:Object references (III)  magnolia:Object references (III)  segnalo:Object references (III)  gifttagging:Object references (III)

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Object references (III)

Leave a Reply