Serialization is a process of converting the state information of an object into a form that can be stored or transmitted. Generally, an object is stored in a storage medium, such as archives or citation buffers. During network transmission, it can be in formats such as bytes or XML. Byte or XML encoding formats can restore completely equal objects. This opposite process is also called deserialization .
Serialization and deserialization of Java objects <br />In Java, we can create objects in many ways, and we can reuse the object as long as the object is not recycled. However, the Java objects we create all exist in the JVM's heap memory. These objects can only exist when the JVM is in running state. Once the JVM stops running, the state of these objects is lost.
However, in real application scenarios, we need to persist these objects and be able to reread the objects when needed. Java object serialization can help us implement this function.
Object serialization mechanism is a built-in object persistence method in Java language. Through object serialization, the state of the object can be saved as a byte array, and this byte array can be converted into an object through deserialization when necessary. Object serialization can be easily converted between active objects and byte arrays (streams) in the JVM.
In Java, the serialization and deserialization of objects are widely used in RMI (remote method calls) and network transmission.
Related interfaces and Java classes provide a convenient API to support it in order to facilitate developers to serialize and deserialize Java objects. This includes the following interfaces and classes:
The class enables its serialization functionality by implementing the java.io.Serializable interface. Classes that do not implement this interface will not be able to serialize or deserialize any of their states. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and is only used to identify serializable semantics. (This interface does not have methods and fields, so why can only objects of the class that implements the interface be serialized?)
When trying to serialize an object, if an object is encountered that does not support the Serializable interface. In this case, a NotSerializableException is thrown.
If the class to be serialized has a parent class, and if you want to persist the variables defined in the parent class at the same time, the parent class should also integrate the java.io.Serializable interface.
Here is a class that implements the java.io.Serializable interface
package com.hollischaung.serialization.SerializableDemos;import java.io.Serializable;/*** Created by hollis on 16/2/17.* Implementing the Serializable interface*/public class User1 implements Serializable {private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "User{" +"name='" + name + '/'' +", age=" + age +'}';}} Serialization and deserialization through the following code
package com.hollischaung.serialization.SerializableDemos;import org.apache.commons.io.FileUtils;import org.apache.commons.io.IOUtils;import java.io.*;/*** Created by hollis on 16/2/17.* SerializableDemo1 Combined with SerializableDemo2 to indicate that a class must implement the Serializable interface if it wants to be serialized*/public class SerializableDemo1 {public static void main(String[] args) {//Initializes The ObjectUser1 user = new User1();user.setName("hollis");user.setAge(23);System.out.println(user);//Write Obj to FileObjectOutputStream oos = null;try {oos = new ObjectOutputStream(new FileOutputStream("tempFile"));oos.writeObject(user);} catch (IOException e) {e.printStackTrace();} finally {IOUtils.closeQuietly(oos);}//Read Obj from FileFile file = new File("tempFile");ObjectInputStream ois = null;try {ois = new ObjectInputStream(new FileInputStream(file));User1 newUser = (User1) ois.readObject();System.out.println(newUser);} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();} finally {IOUtils.closeQuietly(ois);try {FileUtils.forceDelete(file);} catch (IOException e) {e.printStackTrace();}}}}}//OutPut://User{name='hollis', age=23}//User{name='hollis', age=23}The above is all about this article. I hope it will be helpful for everyone to learn object serialization and deserialization in Java.