Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

hashcode and equality

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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 ]
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Fedry Kemilau
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi fedry,

Your bit representation:


Rejoice.
reply
    Bookmark Topic Watch Topic
  • New Topic