Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does java object retain hashcode value when transmitted over the network

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?

like image 342
Victor Avatar asked Oct 28 '13 02:10

Victor


People also ask

Is hashCode memory address in Java?

No, it is just an identifer.

Is Java hashCode consistent?

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.

Can two objects have same hashCode in Java?

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.

Is hashCode always unique in Java?

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.


2 Answers

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.

like image 148
user207421 Avatar answered Sep 20 '22 16:09

user207421


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

like image 23
user439407 Avatar answered Sep 19 '22 16:09

user439407