Re: Hashtables



Roedy Green wrote:

Someone could settle this point with a program that just kept doing a toString() and hashCode on an array of objects and keep doing it to see if they ever change. (You must do something to create and discard objects to trigger GC). I expect toString() will eventually change after a GC but surely hashCode will not.

You may also see if there is a correlation between HashCode (dumped in
hex) and what toString() produces which is some sort of representation
of the address IIRC.

Probably.

"In other words, this method returns a string equal to the value of:

     getClass().getName() + '@' + Integer.toHexString(hashCode())"

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html#toString()

Just checking, Object.toString will use an overridden hashCode method.

I believe IBM JRE uses a different algorithm for creating hashCodes than Sun's. Sun's are predictable (or were), although they have changed their algorithm as well.

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
.



Relevant Pages

  • Re: Why doesnt Integer have a setValue(int i), and is there a way of changing its value.
    ... >> compareTo, toString, and then some. ... > The equals, hashCode, and compareTo methods are associated with value ... Overriding equals, hashCode, and compareTo is ...
    (comp.lang.java.programmer)
  • Re: Why doesnt Integer have a setValue(int i), and is there a way of changing its value.
    ... I thought that whenever you override equals you ... > implement a new hashCode algorithm. ... but this implementation technique is not ... The toString method is often used to ...
    (comp.lang.java.programmer)
  • Re: Hashtables
    ... The Object.hashCode docs mislead. ... toStringand hashCode on an array of objects and keep doing it to ... I expect toString() will eventually ... http://mindprod.com Again taking new Java programming contracts. ...
    (comp.lang.java.help)
  • Re: super.toString() ?
    ... It is guaranteed to return the same string that toString() would have ... returned if neither toStringnor hashCode() had been overridden. ... Javadoc for Object.toStringand System.identityHashCodeare quite ...
    (comp.lang.java.programmer)
  • Re: Hashtables
    ... they don't have a consistent address. ... toStringand hashCode on an array of objects and keep doing it to see if they ever change. ... I expect toString() will eventually change after a GC but surely hashCode will not. ... toString method for class Object returns a string consisting of the name ...
    (comp.lang.java.help)