private static final long serialVersionUID = 1L;
具體的序列化過程是這樣的:java
serialVersionUID有兩種顯示的生成方式: spa
狀況一:假設Person類序列化以後,從A端傳輸到B端,而後在B端進行反序列化。code
Exception in thread "main" java.io.InvalidClassException: com.sf.code.serial.Person; local class incompatible: stream classdesc serialVersionUID = 1234567890, local class serialVersionUID = 123456789 at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:621) at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1623) at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1518) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1774) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371) at com.sf.code.serial.DeserialTest.main(DeserialTest.java:13)
狀況二:假設兩處serialVersionUID一致,對象
狀況三:假設兩處serialVersionUID一致,若是B端減小一個字段,A端不變,會是什麼狀況呢?接口
狀況四:假設兩處serialVersionUID一致,若是B端增長一個字段,A端不變,會是什麼狀況呢?it
說明序列化,反序列化正常,B端新增長的int字段被賦予了默認值0。io