Java object serialization and deserialization
Java object serialization and deserialization
In Java, if we want to save the Instantaneous State values of an object, so that we can get these values in the next use, or persist the object, or use RMI (remote method call ), or when an object is transmitted in the network, we need to serialize the object to achieve serialization. We only need to implement the Serializable interface, which is a Tag interface ), that is, there is no method in it. Its main function is to tell the JVM that objects of this class can be serialized.
Generally, many class objects implement the Serializable interface. However, some objects cannot be serialized, such as database-related connection objects and file objects, it is meaningless to save the status values of these objects. Therefore, this is why the Object does not implement the Serializable interface. to serialize an Object, we only need to implement the Serializable interface, if the parent class of a class implements the Serializable interface, its subclass also implements this interface by default. If its subclass needs to be serialized, its parent class must implement the Serializable interface.
Serialization is used to save the object state. Therefore, static variables are not serialized because it makes no sense to save their values and will change dynamically during running, for variables of the reference type or referenced variables that contain referenced variables, We serialize the object to ensure that they all implement the Serializable interface.
In the serialization process, if we do not want to save the value of some variables, we add a keyword before the referenced variable or original variable name.TransientThis way, the field will not be serialized. When the deserialization is restored to an object, its default value is null (the field is of the object type) or the default value of the original type (the original type variable ).
During deserialization, JVM re-assembles the serialized objects, and then opens up space in heap to generate corresponding types of objects. In this process, it does not call the constructor. Of course, if we do not want to use the default serialization method provided by java, we only need to implement the Externalizable interface. When reading an object, the listless constructor of the serialized class is called to create a new object, and then fill in the Field Values of the saved object into the new object. When this interface is used, the serialization details need to be completed by the programmer.
Example:
package com.test;import java.io.Serializable;/** * Created by siege on 2015-07-28. */public class Person implements Serializable { private transient int num=3; private transient String description=hello; private static int count; private String name; private int age; public Person(String name, int age) { count++; this.name = name; this.age = age; } @Override public String toString() { return name+:+name+,age:+age+,count:+count+,num:+num+,description:+description; }}
package com.test;import java.io.*;/** * Created by siege on 2015-07-28. */public class SerializableTest { public static void main(String[] args) { Person person1=new Person(siege1,20); Person person2=new Person(siege2,21); Person person3=new Person(siege3,22); try { FileOutputStream fos=new FileOutputStream(C:\test\person.out); ObjectOutputStream objectOutputStream=new ObjectOutputStream(fos); objectOutputStream.writeObject(person1); objectOutputStream.writeObject(person2); objectOutputStream.writeObject(person3); objectOutputStream.close(); } catch (java.io.IOException e) { e.printStackTrace(); } try { FileInputStream fin=new FileInputStream(C:\test\person.out); ObjectInputStream objectInputStream=new ObjectInputStream(fin); try { Person p1=(Person)objectInputStream.readObject(); Person p2=(Person)objectInputStream.readObject(); Person p3=(Person)objectInputStream.readObject(); System.out.println(p1); System.out.println(p2); System.out.println(p3); } catch (ClassNotFoundException e) { e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } }}
The output result is as follows:
Name: siege1, age: 20, count: 3, num: 0, description: null
Name: siege2, age: 21, count: 3, num: 0, description: null
Name: siege3, age: 22, count: 3, num: 0, description: null
It can be seen that the static variable value is extracted from the class variable and put into the object after deserialization. At the same time, the transient type variable is not serialized, And the deserialization value is the default value.
If the Person does not implement the Serializable interface
Java. io. NotSerializableException
Error.