Chris Ringer

Greenhorn
+ Follow
since Dec 04, 2006
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
(keep public parts private until JForum day)
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt
Moderation Tools

Recent posts by Chris Ringer

So, just curious, what happens if an object (MyNeatClass version 1.0) is serialized and then before deserialized, someone installs a newer version of the class (MyNeatClass version 1.1) on the system? How does deserialization handle that? Thanks.

Chris
11 years ago
True, Strings are immutable. But what do we care? Reference variables certainly aren't immutable. If using String and garbage collection becomes a performance issue, use StringBuffer or StringBuilder.

Chris
11 years ago
Ok, I see your point. So, basically, if I write a class and the instantiated objects could possibly be put into a concrete Collection of some type (List, Set, Map), I (we?) need to override equals() and therefore hashCode().

And since I don't know how another programmer might be using my class someday, I basically need to override equals() and hashCode(), even if the class is not currently stored in collections. (Who wants to go back, modify code, unit test, system test, redocument and redeploy, so heck - might as well do it up front).

Also, I found out why hashCode() should multiply values by prime numbers. Sun takes our perfectly good hash code and uses that in a supplemental hash() method to compute the final hash code. In doing so, hash() takes our well distributed hash code and inadvertently creates collisions. The prime numbers help counter this unwanted side effect. Here is the Link.

Thanks to all for helping me. Regards,

Chris

[ December 08, 2006: Message edited by: Chris Ringer ]
[ December 08, 2006: Message edited by: Chris Ringer ]
11 years ago
Adne,

Thanks for the nudge. I did find my answer in a SCJP book. The Collection classes that implement interface Set or Map expect the object or key object respectively to override equals( ). And if something overrides equals, it must override HashCode( ) of course. (If two objects are considered equal, they must have the same hashCode).

The List classes don't care. They just operate on index.

Chris
[ December 06, 2006: Message edited by: Chris Ringer ]
11 years ago
The purpose of Generics is to only allow a specific class of objects to stored in a given collection concrete class. For example, I can define an ArrayList so that only objects of my class Foo call be added. So, why in the world did Sun call this Generics? Why wasn't it called Specifics? Just wondering...

The Wikipedia definition of generic is "Generic generally means pertaining or appropriate to large classes or groups as opposed to specific".

Chris
11 years ago

I would say override equals() whenever you have two objects that can be meaningfully equal without being the same instance.


Override hashCode() any time you overide equals()



Ok, thanks for the reply but that didn't clear things up for me...
Let me ask another way. Which methods in which collections will automagically call hashCode() and equals() for me, behind the scene?

A myMap.put(myObject.key, myObject)? a myMap.get()? For a mySet.add() too? What about a mySet.contains()? etc... I'm sure someone out there has a nice matrix they will share so I don't have to reinvent the wheel.

Note that the JVM doesnt handle this (put an object in a bucket).



Huh? If I say myHashSet.put(myObject), I the programmer am certainly not explicitly calling hashCode() on myObject so it must be the compiler / JVM.

bucketIndex = object.hashCode() % array.length;



I'm talking about Collections, not an array. If my hashcode can be a 4 byte int, and my collection size is only 101 buckets, something besides me the programmer figures out how to fit such a large hashCode into much smaller # of buckets.

Regards,

Chris

PS: Where is the "review reply" button on these posts? Or do I just get one chance to get it right?
11 years ago
Hi Everyone,

Forgive me if this is a stupid question. I've read lots and lots of articles on how to override these two methods equals and hashCode in my own object classes but I sure can't find when I should do so. In terms of the Collection concrete classes, are we talking the ones built from Map and Set interfaces? Or should I only be concerned with the classes that have *hash* in the class name? Some please clue me in. Thanks so much.

Chris

PS: What's the deal with using prime numbers in the hashCode() method or making the collection capacity a prime number? Isn't the JVM smart enough to efficiently put an object in a bucket or is this much ado about nothing - an obsolete coding technique?
11 years ago