Object references (III)
3 March 2008This 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.
Related Posts:
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)