If I have a serializable java object where hashcode method is not over ridden, and then i transmit the java object over the netwrok and on the receiving end, reconstruct the java object, then will the hashcode method return the same value ?
Is it something specified by the Java API or implementation dependent based on the JVM?
No, it is just an identifer.
Whenever it is invoked on the same object more than once during an execution of a Java application, hashCode() must consistently return the same value, provided no information used in equals comparisons on the object is modified.
1) If two objects are equal (i.e. the equals() method returns true), they must have the same hashcode. 2) If the hashCode() method is called multiple times on the same object, it must return the same result every time. 3) Two different objects can have the same hash code.
The hashcode is always the same if the object doesn't change. Hashcode is a unique code generated by the JVM at time of object creation. It can be used to perform some operation on hashing related algorithms like hashtable, hashmap etc.
There is nothing in the Java Language Specification, Java Virtual Machine Specification, or Object Serialization Specification that says so. So you can't rely on it, whatever behaviour you may happen to observe in any particular case.
It depends, as others have said if you are depending on transient fields then the value of the hashcode will be different, but it also depends on how your hashcode is implemented. If your hashcode method depends on the hashcode of objects that either use transient objects or the default method(or itself depends on objects that use those) then the hashcode may very well end up different.
Although not specified by the standards, the default hashcode for objects usually just returns their internal address, and this will obviously be different depending on which machine the jvm is running on..... Overall I would not depend on any hashcode method that you do not control returning the same thing after being serialized/deserialized
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With