您的位置 首页 java

transient关键字的作用是什么

transient关键字的作用是:被transient修饰的变量不参与序列化和反序列化。当一个对象被序列化的时候,transient型变量的值不包括在序列化的表示中,然而非transient型的变量是被包括进去的。

本教程操作环境:windows7系统、java10版、Dell G3电脑。

(学习视频分享:java教学视频)

Java的serialization提供了一种持久化对象实例的机制。当持久化对象时,可能有一个特殊的对象数据成员,我们不想用serialization机制来保存它。

为了在一个特定对象的一个域上关闭serialization,可以在这个域前加上关键字transient。

当一个对象被序列化的时候,transient型变量的值不包括在序列化的表示中,然而非transient型的变量是被包括进去的。

简而言之,被transient修饰的变量不参与序列化和反序列化。

接下来用代码来证明一下。

新建一个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类里边测试。

PS:

为了代码简洁这里的IO操作没有进行try catch操作而是直接抛出了。

public class TestTransient {    public static void main(String[] args) throws Exception {        // 实例化一个Student对象.        Student student = new Student(15, "HuaGe");        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属性从HuaGe变为了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;}

这个接口的两个方法可以指定对类中的哪些属性进行序列化。

使用这个接口时,无论属性有没有被transient修饰,

默认不对任何属性进行序列化。所以实现了Externalizable接口的类

一般不再使用transient修饰属性。

总结:

1. 被transient修饰的变量不参与序列化和反序列化

2. transient一般在实现了Serializable接口的类中使用。

文章来源:智云一二三科技

文章标题:transient关键字的作用是什么

文章地址:https://www.zhihuclub.com/14179.shtml

关于作者: 智云科技

热门文章

网站地图