Java基础之IO篇——Serializable
这篇博客是为了记录刚开始写java时的疑惑:为什么数据库表对应的java实体类都implements Serializable 接口?且声明了一个serialVersionUID = -113873284728278L?
要知道为什么实体类要去实现这个接口,首先要知道这个接口是干嘛的,有什么作用。Serializable是java.io包下的接口,查看java官方类库文档对Serializable接口的说明是这样的:
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.
大意就是实现了Serializable接口的类及其子类的对象可以被序列化和反序列化,Serializable接口没有方法和字段,只用于标识可序列化语义。
那么什么是序列化和反序列化?
序列化:将对象转化为字节序列的过程。