java serializable_Java 之 Serializable

作用场景:

内存中的对象状态保存到一个文件中或者数据库;

套接字在网络上传送对象;

通过RMI传输对象;

对象-字节 序列化

字节-对象 反序列化

Exception in thread "main" java.io.InvalidClassException: FlyPig;

local class incompatible:

stream classdesc serialVersionUID = 8191931239315631084,

local class serialVersionUID = -7757104472191332224

版本升级,旧数据兼容

实现这个Serializable 接口的时候,一定要给这个 serialVersionUID 赋值

public class FlyPig implements Serializable {

private static final long serialVersionUID = 1L;

https://github.com/erinchenff/learn_after_MrQi/blob/master/src/test/java/TestSerialDemo.java

/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/rt.jar!/java/io/Serializable.class

* Serializability of a class is enabled by the class implementing the

* java.io.Serializable interface. Classes that do not implement this

* interface will not have any of their state serialized or

* deserialized. All subtypes of a serializable class are themselves

* serializable. The serialization interface has no methods or fields

* and serves only to identify the semantics of being serializable.

类通过实现 java.io.Serializable 接口以启用其序列化功能。

未实现此接口的类将无法使其任何状态序列化或反序列化。

可序列化类的所有子类型本身都是可序列化的。因为实现接口也是间接的等同于继承。

序列化接口没有方法或字段,仅用于标识可序列化的语义。标识接口。通知jvm,我不对这个类做序列化了,你(jvm)帮我序列化就好了

静态变量不会被序列化( static,transient)

当一个父类实现序列化,子类自动实现序列化,不需要显式实现Serializable接口。

当一个对象的实例变量引用其他对象,序列化该对象时也把引用对象进行序列化。

serialVersionUID

* The serialization runtime associates with each serializable class a version

* number, called a serialVersionUID, which is used during deserialization to

* verify that the sender and receiver of a serialized object have loaded

* classes for that object that are compatible with respect to serialization.

* If the receiver has loaded a class for the object that has a different

* serialVersionUID than that of the corresponding sender's class, then

* deserialization will result in an {@link InvalidClassException}. A

* serializable class can declare its own serialVersionUID explicitly by

* declaring a field named "serialVersionUID" that must be static,

* final, and of type long:

*

*

 
 

* ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;

*

*

* If a serializable class does not explicitly declare a serialVersionUID, then

* the serialization runtime will calculate a default serialVersionUID value

* for that class based on various aspects of the class, as described in the

* Java(TM) Object Serialization Specification. However, it is strongly

* recommended that all serializable classes explicitly declare

* serialVersionUID values, since the default serialVersionUID computation is

* highly sensitive to class details that may vary depending on compiler

* implementations, and can thus result in unexpected

* InvalidClassExceptions during deserialization. Therefore, to

* guarantee a consistent serialVersionUID value across different java compiler

* implementations, a serializable class must declare an explicit

* serialVersionUID value. It is also strongly advised that explicit

* serialVersionUID declarations use the private modifier where

* possible, since such declarations apply only to the immediately declaring

* class--serialVersionUID fields are not useful as inherited members. Array

* classes cannot declare an explicit serialVersionUID, so they always have

* the default computed value, but the requirement for matching

* serialVersionUID values is waived for array classes.

序列化运行时使用一个称为 serialVersionUID 的版本号与每个可序列化类相关联,该序列号在反序列化过程中用于验证序列化对象的发送者和接收者是否为该对象加载了与序列化兼容的类。如果接收者加载的该对象的类的 serialVersionUID 与对应的发送者的类的版本号不同,则反序列化将会导致 InvalidClassException。可序列化类可以通过声明名为 “serialVersionUID” 的字段(该字段必须是静态 (static)、最终 (final) 的 long 型字段)显式声明其自己的 serialVersionUID:

如果可序列化类未显式声明 serialVersionUID,则序列化运行时将基于该类的各个方面计算该类的默认 serialVersionUID 值,如“Java™ 对象序列化规范”中所述。不过,强烈建议 所有可序列化类都显式声明 serialVersionUID 值,原因是计算默认的 serialVersionUID 对类的详细信息具有较高的敏感性,根据编译器实现的不同可能千差万别,这样在反序列化过程中可能会导致意外的 InvalidClassException。因此,为保证 serialVersionUID 值跨不同 java 编译器实现的一致性,序列化类必须声明一个明确的 serialVersionUID 值。还强烈建议使用 private 修饰符显示声明 serialVersionUID(如果可能),原因是这种声明仅应用于直接声明类 – serialVersionUID 字段作为继承成员没有用处。数组类不能声明一个明确的 serialVersionUID,因此它们总是具有默认的计算值,但是数组类没有匹配 serialVersionUID 值的要求。

参考 https://blog.csdn.net/u013870094/article/details/82765907

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值