面试官:Java 序列化和反序列化为什么要实现 Serializable 接口?
序列化和反序列化
序列化:把对象转换为字节序列的过程称为对象的序列化. 反序列化:把字节序列恢复为对象的过程称为对象的反序列化.
什么时候需要用到序列化和反序列化呢?
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
/** The value is used for character storage. */
private final char value[];
/** Cache the hash code for the string */
private int hash; // Default to 0
/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = -6849794470754667710L;
......
}
<insert id="insertUser" parameterType="org.tyshawn.bean.User">
INSERT INTO t_user(name, age) VALUES (#{name}, #{age})
</insert>
实现序列化和反序列化为什么要实现Serializable接口?
实现Serializable接口就算了, 为什么还要显示指定serialVersionUID的值?
public class User implements Serializable {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
(2) 测试类
先进行序列化, 再进行反序列化.
public class SerializableTest {
private static void serialize(User user) throws Exception {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("D:\\111.txt")));
oos.writeObject(user);
oos.close();
}
private static User deserialize() throws Exception{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("D:\\111.txt")));
return (User) ois.readObject();
}
public static void main(String[] args) throws Exception {
User user = new User();
user.setName("tyshawn");
user.setAge(18);
System.out.println("序列化前的结果: " + user);
serialize(user);
User dUser = deserialize();
System.out.println("反序列化后的结果: "+ dUser);
}
}
(3) 结果
先注释掉反序列化代码, 执行序列化代码, 然后User类新增一个属性sex
public class User implements Serializable {
private String name;
private Integer age;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}
}
再注释掉序列化代码执行反序列化代码, 最后结果如下:
序列化前的结果: User{name='tyshawn', age=18}
Exception in thread "main" java.io.InvalidClassException: org.tyshawn.SerializeAndDeserialize.User; local class incompatible: stream classdesc serialVersionUID = 1035612825366363028, local class serialVersionUID = -1830850955895931978
报错结果为序列化与反序列化产生的serialVersionUID不一致.
接下来我们在上面User类的基础上显示指定一个serialVersionUID
private static final long serialVersionUID = 1L;
再执行上述步骤, 测试结果如下:
序列化前的结果: User{name='tyshawn', age=18}
反序列化后的结果: User{name='tyshawn', age=18, sex='null'}
显示指定serialVersionUID后就解决了序列化与反序列化产生的serialVersionUID不一致的问题.
Java序列化的其他特性
先说结论, 被transient关键字修饰的属性不会被序列化, static属性也不会被序列化.
我们来测试下这个结论:
(1) User类
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private Integer age;
private transient String sex;
private static String signature = "你眼中的世界就是你自己的样子";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public static String getSignature() {
return signature;
}
public static void setSignature(String signature) {
User.signature = signature;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", sex='" + sex +'\'' +
", signature='" + signature + '\'' +
'}';
}
}
(2) 测试类
public class SerializableTest {
private static void serialize(User user) throws Exception {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("D:\\111.txt")));
oos.writeObject(user);
oos.close();
}
private static User deserialize() throws Exception{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("D:\\111.txt")));
return (User) ois.readObject();
}
public static void main(String[] args) throws Exception {
User user = new User();
user.setName("tyshawn");
user.setAge(18);
user.setSex("man");
System.out.println("序列化前的结果: " + user);
serialize(user);
User dUser = deserialize();
System.out.println("反序列化后的结果: "+ dUser);
}
}
序列化前的结果: User{name='tyshawn', age=18, sex='man', signature='你眼中的世界就是你自己的样子'}
反序列化后的结果: User{name='tyshawn', age=18, sex='null', signature='我的眼里只有你'}
因为序列化是针对对象而言的, 而static属性优先于对象存在, 随着类的加载而加载, 所以不会被序列化.
看到这个结论, 是不是有人会问, serialVersionUID也被static修饰, 为什么serialVersionUID会被序列化? 其实serialVersionUID属性并没有被序列化, JVM在序列化对象时会自动生成一个serialVersionUID, 然后将我们显示指定的serialVersionUID属性值赋给自动生成的serialVersionUID.
作者:椰子Tyshawn
来源:blog.csdn.net/litianxiang_kaola/article/details/100097019
版权申明:内容来源网络,仅供分享学习,版权归原创者所有。除非无法确认,我们都会标明作者及出处,如有侵权烦请告知,我们会立即删除并表示歉意。谢谢!