• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

hashCode rulez

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.
I read the following:
A class C has legal implementations of the equals and hashCode methods. On Monday, an instance of the class is created and the hashCode method is invoked. On Tuesday the program is loaded again and an instance of class C is created containing the same data that was loaded on Monday. If the hashCode method is invoked after restarting the program on Tuesday then the hashCode method must return the same integer value that was returned on Monday.
a. false
b. true

AND THE ANSWERE WAS a!
I don't know why.
I read the opposite in every book.
Pls. help.
BYE
DD
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the API:
Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
So there you have it. It must be consistent within a single run of the application.
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
This is because of the contract of the hashCode() method. Know more about it here.
[ June 19, 2003: Message edited by: Anupam Sinha ]
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually hashcode() method returns the decimal value wat we get if we print the object directly which prinths the class name@ the hexacode. And this hash code is given to the particular object at compile time. and u will get may be new hashcode for a same object after every compilation & if ur class file is changed.
with rgds,
Manish
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Manish

and u will get may be new hashcode for a same object after every compilation & if ur class file is changed.


I don't think that only if a class file is changed then you may get a different value. Even if the class file is not chnaged then also the value may differ.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Denes Doma
i think that the key to the answer may be here:

A class C has legal IMPLEMENTATIONS of the equals and hashCode methods.


here the class C is an user self-defined class and it maybe get the "Date" as one field when it implement the hashCode method of Object class. then it is different to any other system classes which has hashCode() you read the opposite in every book.
 
reply
    Bookmark Topic Watch Topic
  • New Topic