This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hashcode and equality Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "hashcode and equality" Watch "hashcode and equality" New topic
Author

hashcode and equality

Fedry Kemilau
Ranch Hand

Joined: Aug 06, 2007
Posts: 44



and the result is:

hashcode OK

===========
hashcode OK
This is clover dog

===========
hashcode OK
null

===========
hashcode OK
equality OK
This is clover dog




Question:
Why in the line 1, the code does not check for equality?

Thank you
[ August 11, 2007: Message edited by: fedry kemilau ]

Solve this code:<br /> <br />10010101100001111011011000010100000111001011011111100011110101111100110100001
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi fedry kemilau,

I got you. but after carefully observing your code 15 mins.
Its not that easy for me.

Now into details,

1. Dog d = new Dog("clover");
This line looks simple; but key to whole explanation;

here we are creating Dog in heap with state name = clover;



2. m.put(d, "This is clover dog");
Here d.hashCode() calulated and it will be placed in corresponding bucket.

hashCode() = 6 (length of string).
So its placed in 6th bucket.

Here what need to understood is
Object will only be there in Heap.
just reference will be put in map.


System.out.println("\n===========");

3. System.out.println(m.get(d));// line 1

Here to find which bucket d belongs, it will calculate hashCode on it.
Here d still points to "clover"; so hashCode = 6;

Now we are in 6th bucket;
it will iterate on elements in bucket 6 and takes fisrt object reference.

*** here both the references we passed are same.
so no need to call equals.


Thats why we got the value "This is clover Dog"

4. d.name = "magnolia";

// here the object state on heap changed to "magnolia".
System.out.println("\n===========");


5.System.out.println(m.get(d));// line 2
// hashCode(d) --> 8(length of "magnolia".)
but it founds nothing in the 8th bucket return null.
***Remember, we have only one element in 6th element.


6. d.name = "argume";
Again changing the state of object;
**** Here you are placing d reference in pool. useful in step 7

System.out.println("\n===========");

7. System.out.println(m.get(new Dog("argume"))); // line 3

// This is very complex to understand; prepare for this;

you are now creating another Dog in heap with state "argume";
hashCode returns 6;

so in 6th bucket it will iterate and fetches the first element
here map element's d reference and new Dog("argume")'s references are differnt.

So executes equals() method.
// your equals definition is very poor. not efficient.
// you are comparing both dog object's references. not contents.
Here you need String's pool concept to understand
why reference( Map element's d.name ) == reference(new Dog("argume").name)

reference( Map element's d.name ) >>>
1. Map has only reference to Dog Object; its state changed in 6th step as d.name = "argume";

"argume" will be created in heap and reference placed in pool.
reference(new Dog("argume").name)
2. second time when you refer "argume" it will check in pool and gives that reference[ no new string "argume" created here]

Hence reference( Map element's d.name ) == reference(new Dog("argume").name)

Thanks for your post. which made me think.
[ August 11, 2007: Message edited by: Srinivasan thoyyeti ]

Thanks & Regards,<br />T.Srinivasan,<br />SCWCD 1.4(89%),SCJP 5.0(75%)<br />"That service is the noblest which is rendered for its own sake." - Mahatma Gandhi
Fedry Kemilau
Ranch Hand

Joined: Aug 06, 2007
Posts: 44
So, the point is if we are using the same reference variable as the get() method's argument, it will not check for the equality, isn't it?
Hmm... I think I have to review again the String pool concept.

Thanks a lot for the explanation.
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
You are welcome.

But whats that
Solve this code:

100101011000011110110110000110000011100101101111110001111010111110011100001

How did you get this ?
we can only use shift operators on integers.
highest available length is Long 8 bytes.

But that is more than 8 bytes.
How did you get that?
I guess you simply wrote that
Any hidden trick ?
[ August 11, 2007: Message edited by: Srinivasan thoyyeti ]
Fedry Kemilau
Ranch Hand

Joined: Aug 06, 2007
Posts: 44
lol, I did that stuff when i was bored.

Those actually come from String --> char[] --> int[] --> then these ints are converted into binary. Try it if you got nothing to do.
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by fedry kemilau:
Those actually come from String --> char[] --> int[] --> then these ints are converted into binary.
Tip: There is an easier way to work with big integers
Fedry Kemilau
Ranch Hand

Joined: Aug 06, 2007
Posts: 44
Hmm.. what is big integer? I think it is not related at all.
What i am doing is just convert; let's say 'a' ==> 1100001, ' ' ==> 0100000.

But looks like Big Integer is a useful thing, gotta read the API.
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi fedry,

Your bit representation:


Rejoice.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: hashcode and equality
 
Similar Threads
collections topic(map)
Please explain HashMap?
Hashmap doubt from K&B
equals() doesn't fail for 2 different strings of the same hashCode Pages 583, 584, 585 of SCJP6
K & B - Generics & Collections