关键字transient

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修饰,它在序列化时会被直接跳过。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

痴书先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值