1. What is serialization and deserialization
Serialization is a process of converting an object into a byte stream; deserialization is the process of reverting a byte stream to an object.
2. What situations require serialization
A) When you want to save the object in memory in a file or in a database;
b) When you want to use sockets to transfer objects on the network;
c) When you want to transfer objects through RMI;
3. How to implement serialization
It is possible to implement the serializable interface for a class that requires serialization, and the Serializable interface, like the Cloneable interface, does not contain any methods, it is a labeled interface.
4. Code Analysis
Package Com.tonyluis;import java.io.*;p Ublic class Solution {public static void main (String args[]) {ObjectOutputStream o Bjectos = null; Serializabletest myTest = new Serializabletest ("str", 1, "123456", 8); try {objectos = new ObjectOutputStream (New Fileo Utputstream ("Test.dat")); Objectos.writeobject (myTest); Objectos.flush (); Objectos.close ();} catch (FileNotFoundException e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Au To-generated catch Blocke.printstacktrace ();} ObjectInputStream Objectin; Serializabletest mts = null;try {objectin = new ObjectInputStream (New FileInputStream ("Test.dat")); MTS = ( serializabletest) Objectin.readobject ();} catch (FileNotFoundException e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Au To-generated catch Blocke.printstacktrace ();} catch (ClassNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();} SYSTEM.OUT.PRINTLN (MTS);//Note that the Equals () method is not overridden, and the object classEquals () defaults to the address comparison System.out.println (Mts.equals (MyTest));}} Class Serializabletest implements Serializable {//serialization ID provides two build strategies under Eclipse//One is a fixed 1L, and one is randomly generating a non-repeating long type of data (actually using JDK tool Generation)//If there is no special requirement, it is to use the default 1L can be static final long serialversionuid = 1L; The string name;int num;static int staticnum;//transient keyword is a transient string pwd;transient int NUM0 that cannot be serialized; Serializabletest (string name, int num, int staticnum, String pwd, int num0) {this.name = Name;this.num = Num;this.staticnu m = staticnum;this.pwd = PWD;THIS.NUM0 = NUM0;} Public String toString () {return "name=" + name + ", num=" + num + ", staticnum=" + Staticnum + ", pwd=" + pwd + ", num0=" + N UM0;}}
Output Result:
Name=str,num=12,staticnum=1,pwd=null,num0=0
False
5. Relationship of pre-and post-serialized objects
After deserializing the restored object address differs from the original address, the address of the object before and after the serialization is different, but the content is the same, and the object contains the same references. In other words, with serialization, we can implement deep copy of any serializable object-meaning we replicate the entire object net, not just the base object and its references. For the same-class objects, their addresses are the same, stating that they are the same object, but not the same as the object addresses of other streams. It is also said that as long as the object is serialized into a single stream, it can recover the same object net as we wrote it, and as long as the object is the same in the same stream.
Serializable interface transient keywords in Java, and object io