• Post Reply Bookmark Topic Watch Topic
  • New Topic

hashcode question  RSS feed

 
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

Does the hashcode() of the object return the same number for different executions?
I am calculating the hashcode of the object to see if the object is changed.
this works fine as long as i dont restart my server(App Server). But, once i restart the server the hashcode of the object changes even though the original object doesn't change. Is this the way the hashcode works?

Is there any other way to see if the object is changed?

Thanks,
 
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Srikanth,
Can you post the hashCode implementation? Maybe it is using Date() or some other value the changes?

hashCode() is supposed to be implemented to return the same value when the same fields are set.

Do you want to know whether the object has changed or if it is different. These are distinct things. For example, suppose my object has a field set to 3. I then change it to 5. At this point, it has changed. Now I change it back to 3. Should it be considered changed?
 
Saathvik Reddy
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeanne Boyarsky:
Srikanth,
Can you post the hashCode implementation? Maybe it is using Date() or some other value the changes?

hashCode() is supposed to be implemented to return the same value when the same fields are set.

Do you want to know whether the object has changed or if it is different. These are distinct things. For example, suppose my object has a field set to 3. I then change it to 5. At this point, it has changed. Now I change it back to 3. Should it be considered changed?


My hashCode() implementation doesn't use date


Let me give some more details about my problem:
I am saving the hashCode() of the object to database when the object is created. Every time the object changes i save it's hashCode to database.
This works fine as long as i don't restart my server.
But, when i restart my server even though the object isn't changed i still get different hashCode(). I read that hashCode() is an implementation of address location of the object. Does it gaurentee that the object is loaded at the same memory address on each execution?
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Jeanne]: hashCode() is supposed to be implemented to return the same value when the same fields are set.

The hashCode() is not required to be the same if the JVM is stopped and restarted. From Object.hashCode():
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.

In particular, the implementation of hashCode() from Object may well return different results on different executions, as it's generally based off of the memory address used for the object.

What data type is this.oid? And what does QuestionValueVersion's hashCode() look like? I suspect that the latter may be inherited directly from Object, and that's why your hashcodes change from one execution to the next.
 
Saathvik Reddy
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:

What data type is this.oid? And what does QuestionValueVersion's hashCode() look like? I suspect that the latter may be inherited directly from Object, and that's why your hashcodes change from one execution to the next.


oid is of type String. Impl. of QuestionValueVersion's hashCode

value is of type String.

Thanks,
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hm, if QuestionValueVersion's.oid is also a String, then I don't know where the changing values are coming from. since Strings have hash codes dependent entirely on content, which should not change from one execution to the next. If you really want to know where the changes are coming from, you may need to print more diagnostic info each time. Show the hash codes of each oid , QuestionValueVersion, and value, to see which are changing and which remain constant.

Alternately, you can just accept that hashCode() is not generally designed to remain constant from one execution to the next (even though it often does), and so you should not use hashCode() to detect changes as you're doing now. Perhaps you can serialize the objects to preserve their values from one invocation to the next, and deserialize to recover the old value. Then you can use methods like equals() or perhaps compareTo() to compare the old object to other objects.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!