Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can HashMap serialized in 1.7, be used in 1.6?

I thought this would throw an error saying minor version number; But it didn't. And is working correctly. Can anyone point out, why does this work?

I created a hashMap in application running on java 1.7, serialized it and sent it to an application using jdk 1.6. The receiver application was able to get the contents without any error.

like image 576
user3240364 Avatar asked Dec 14 '22 20:12

user3240364


1 Answers

This is where the serialVersionUID attribute comes into play.

In the HashMap class both the Java version 6.0 and 7.0 the serialVersionUID is the following:

private static final long serialVersionUID = 362498820763181265L;

This means no changes were made to the class which would change how the object is serialized therefore you get no error if different versions of Java was used to serialize/deserialize the object, both are able to serialize/deserialize the object properly.

Moreover, even Java 8.0 has the same serial version UID for HashMap and also Java 5.0 (and even 1.4), which means you can read/write HashMaps with Java 1.4-5-6-7-8 without any problems.

like image 135
icza Avatar answered Dec 17 '22 10:12

icza