国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Java序列化中的serialVersionUID有什么用?

2019-11-15 00:26:57
字體:
來源:轉載
供稿:網友
java序列化中的serialVersionUID有什么用?

如果一個實現了Serializable的類沒有serialVersionUID屬性,IDE(比如Eclipse)通常會報這樣一個warning:

The serializable class Foo does not declare a static finalserialVersionUID field of type long

那這個serialVersionUID是做什么用的呢?可以看看JDK中Serializable接口的注釋。

The serialization runtime associates with each serializable class aversion number, called a serialVersionUID, which is used duringdeserialization to verify that the sender and receiver of aserialized object have loaded classes for that object that arecompatible with respect to serialization. If the receiver has loadeda class for the object that has a different serialVersionUID thanthat of the corresponding sender's class, then deserialization willresult in an InvalidClassException}. A serializable class candeclare its own serialVersionUID explicitly by declaring a fieldnamed "serialVersionUID" that must be static, final, andof type long:

serialVersionUID 表示可序列化類的版本,在反序列化對象時,用來確認序列化與反序列化該對象所使用的類的版本是否兼容。如果類的版本不一致,那么反序列化將不能正常進行,拋出InvalidClassException。

If a serializable class does not explicitly declare aserialVersionUID, then the serialization runtime will calculate adefault serialVersionUID value for that class based on variousaspects of the class, as described in the Java(TM) ObjectSerialization Specification. However, it is stronglyrecommended that all serializable classes explicitly declareserialVersionUID values, since the default serialVersionUIDcomputation is highly sensitive to class details that may varydepending on compiler implementations, and can thus result inunexpected InvalidClassExceptions duringdeserialization. Therefore, to guarantee a consistentserialVersionUID value across different java compilerimplementations, a serializable class must declare an explicitserialVersionUID value. It is also strongly advised that explicitserialVersionUID declarations use the PRivate modifierwhere possible, since such declarations apply only to the immediatelydeclaring class--serialVersionUID fields are not useful as inheritedmembers. Array classes cannot declare an explicit serialVersionUID,so they always have the default computed value, but the requirementfor matching serialVersionUID values is waived for array classes.

如果一個可序列化的類沒有包含serialVersionUID,運行時會根據這個類的特征自動計算出一個serialVersionUID。那么,為什么不能用默認的這個實現呢,似乎更省事?因為不同的編譯器實現會導致同一個類的源代碼文件,被計算出不同的serialVersionUID.

StackOverflow上有一個類似的問題:http://stackoverflow.com/questions/285793/what-is-a-serialversionuid-and-why-should-i-use-it點擊下面這個鏈接,可以查看更多關于Serializable接口的信息:https://github.com/ZhaoX/jdk-1.7-annotated/blob/master/src/java/io/Serializable.java


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 荃湾区| 庆云县| 达州市| 博爱县| 合肥市| 米林县| 龙南县| 韶山市| 嵩明县| 什邡市| 兖州市| 治多县| 密山市| 福建省| 平山县| 额尔古纳市| 抚顺市| 东光县| 陕西省| 泽普县| 铜川市| 怀柔区| 塔河县| 绿春县| 六安市| 犍为县| 施甸县| 阿瓦提县| 阳新县| 锡林浩特市| 蓝田县| 白朗县| 双流县| 汕头市| 将乐县| 陇南市| 兴隆县| 连江县| 开封市| 连江县| 阳曲县|