Java的serialization提供了一种持久化对象实例的机制。当持久化对象时,可能有一个特殊的对象数据成员,我们不想用serialization机制来保存它。
为了在一个特定对象的一个域上关闭serialization,可以在这个域前加上关键字transient。
当一个对象被序列化的时候,transient型变量的值不包括在序列化的表示中,然而非transient型的变量是被包括进去的。
简而言之,被
t
r
a
n
s
i
e
n
t
修
饰
的
变
量
不
参
与
序
列
化
和
反
序
列
化
\color{red}{transient修饰的变量不参与序列化和反序列化}
transient修饰的变量不参与序列化和反序列化。
接下来用代码来证明一下。
eg1:
新建一个Student类实现Serializable 接口,并重写其toString方法便于观察结果。
一个age属性不被transient修饰,一个name属性被transient修饰。
public class Student implements Serializable {
private int age;
private transient String name;
public Student() {
}
public Student(int age, String name) {
this.age = age;
this.name = name;
}
@Override
public String toString() {
return "Student{" + "age=" + age + ", name='" + name + '\'' + '}';
}
}
然后在TransientTest类里边测试。为了代码简洁这里的IO操作没有进行try catch操作而是直接抛出了。
public class TestTransient {
public static void main(String[] args) throws Exception {
// 实例化一个Student对象.
Student student = new Student(15, "tom");
System.out.println(student);
// 将student对象写入磁盘文件(序列化)
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student.txt"));
oos.writeObject(student);
oos.close();
// 从磁盘文件读取student对象(反序列化)
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("student.txt"));
student = (Student) ois.readObject();
System.out.println(student);
}
}
运行main方法,观察控制台打印信息。
发现经过了序列化和反序列化后,name属性从tom变为了null.
这就说明了被transient修饰的变量不参与序列化和反序列化。
那有没有例外呐?
我们知道,java中有两种序列化的方式
1. 实现Serializable接口。
2. 实现Externalizable接口。
Externalizable接口是Serializable接口的子类
源码如下
public interface Externalizable extends java.io.Serializable {
void writeExternal(ObjectOutput out) throws IOException;
void readExternal(ObjectInput in) throws IOException, ClassNotFoundException;
}
Externalizable接口的两个方法可以指定对类中的哪些属性进行序列化。使用这个接口时,无论属性有没有被transient修饰,默认不对任何属性进行序列化。所以实现了Externalizable接口的类一般不再使用transient修饰属性。
总结:
1. 被transient修饰的变量不参与序列化和反序列化
2. transient一般在实现了Serializable接口的类中使用。
eg2:
下面的程序定义了一个Usere类,它包含用户名,密码、年龄,其中密码和年龄被transient关键字修饰,此外,该类还实现了 Serializable 接口。
public class User implements Serializable {
private static final long serialVersionUID = 3601470141407549788L;
private String userName;
private transient String password;
private transient int age;
public User(String userName, String password, int age) {
this.userName = userName;
this.password = password;
this.age = age;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
编写一个测试类test
@Test
public void test25(){
ObjectOutputStream oos = null;
User user = new User("章子怡","123456",25);
try {
oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("ObjectData")));
oos.writeObject(user);
System.out.println("保存对象前: ");
System.out.println("用户姓名: "+user.getUserName());
System.out.println("用户密码: "+user.getPassword());
System.out.println("用户年龄: "+user.getAge());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream("ObjectData")));
try {
User user1 = (User)ois.readObject();
System.out.println("保存对象后: ");
System.out.println("用户姓名: "+user1.getUserName());
System.out.println("用户密码: "+user1.getPassword());
System.out.println("用户年龄: "+user1.getAge());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
总结:
transient用于修饰不需要序列化的字段,如果一个引用类型被transient修饰,则其反序列化的值为null,如果一个基本类型被transient修饰,则其反序列化的值为0
,如果字段的引用类型是不可序列化的类,则也应该使用transient修饰,它在序列化时会被直接跳过。