Help coderanch get a
new server
by contributing to the fundraiser
  • 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Implementing stock methods

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"They" say that any "serious" class should override or implement toString(), equals(), hashCode(), and the Cloneable and Serializable interfaces.
Some of these are easy enough:

How do you implement hashCode()? It is a native method of the class java.lang.Object; do you just "get" it through inheritance (then why are they saying to implement it) or do you have to do something and what do you do?
Steve
sgwbutcher@aol.com

[This message has been edited by Steve Butcher (edited March 22, 2000).]
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One thing about objects are:
Objects have state and indentity.
Is the method hashCode() provided to give identity to an object?
Coming to the question, we are supposed to implement hashCode() if we implement equals(). The method hashCode() has to return same value for two objects if calling obj1.equals(obj2) returns true. It also has to return same value when called on same object many times.
I wonder how we can do this.

 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the hashCode() method can be just overriden without any problems.
The standart implementation of equals() method does the following:
boolean equals(a) {
return ((a.hashCode() == this.hashCode()) && (a == this));
}

So, the idea of hashCoding is to be able to tell some comparing stuff that object DO differ. So, if the hashCode()s are different and the implementation of hashCode() method is supposed to be very fast, at least faster than byte-to-byte comparison, we can know that the object we search is wrong.
Anyhow, there can be definetely be two objects with the same hashCodes. For example, Long classs.

------------------
With best of best regards, Pawel S. Veselov ( aka Black Angel )
 
Steve Butcher
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pawel,
I am now beginning to understand more fully the ramifications of equals() and hashCode(). However, clearly Object.hashCode() does not meet your criteria for subclasses--it would cause your equals() method to fail for objects that meet the criteria of being equals(). For example,

Results in the following output:

Clearly the first condition, that for every hashCode() on the same object, hashCode() returns the same result during a single run of the application BUT for two objects that are equals(), in this case t[0] and t[2], Object.hashCode() does not return the same hashCode() so this violates the general contract of the hashCode() API.
Obviously, this is because hashCode() has not been overridden to provide appropriate hashcodes for Test objects. I have, using the greatest of all teachers, javac, discovered that you can override a method in base class that was declared native (that was one basic question I had). Now, my basic question is: How do you compute a hashcode? On what should the hashcode be computed?
Thank you all for your assistance; I should have been more precise in my questions.
Steve
sgwbutcher@aol.com
[PS I'm sorry Thandapani but I really don't know what you're getting at, I have read and understood the API, it has not helped. I just don't know how to implement a proper hashCode() method based on the general contract or any contract for that matter. That is the help I am looking for. ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic