Serializable序列化

serialVersionUID

运行时判断类的serialVersionUID来验证版本的一致性;

  • serialVersionUID不一致,会报invalidClassException;
  • serialVersionUID一致,缺少字段不处理,多余字段会赋默认值;
  • 如果不显式的设置serialVersionUID,jvm内部默认根据类的属性计算值,类一旦序列化后,就不能修改了,计算的serialVersionUID会改变,导致反序列化失败;

java序列化与json序列化

场景

1、内存对象写入硬盘;
2、网络传输序列化对象;

Q&A

1、What is the difference between Serializable and Externalizable interface in Java?

2、How many methods Serializable has? If no method then what is the purpose of Serializable interface?

3、What is serialVersionUID? What would happen if you don’t define this?

4、While serializing you want some of the members not to serialize? How do you achieve it?

5、What will happen if one of the members in the class doesn’t implement Serializable interface?

6、If a class is Serializable but its super class in not, what will be the state of the instance variables inherited from super class after deserialization?

7、Can you Customize Serialization process or can you override default Serialization process in Java?

8、Suppose super class of a new class implement Serializable interface, how can you avoid new class to being serialized?

9、Which methods are used during Serialization and DeSerialization process in Java?

10、Suppose you have a class which you serialized it and stored in persistence and later modified that class to add a new field. What will happen if you deserialize the object already serialized?

11、What are the compatible changes and incompatible changes in Java Serialization Mechanism?

12、Can we transfer a Serialized object vie network?

13、Which kind of variables is not serialized during Java Serialization?