Two ways to implement serialization in Java Serializable interface and externalizable interface
The serialization of an object is the writing of an object to the output stream.
Deserialization is the reading of an object from an input stream.
The classes used to implement serialization are in the Java.io package, and the classes or interfaces we use are:
ObjectOutputStream: Provides a way to serialize an object and write it to the stream
ObjectInputStream: Reading the stream and deserializing the object
Serializable: An object wants to be serialized, then its class implements this interface, and all properties of the object (including the private property, including its reference object) can be serialized and deserialized to save, pass.
externalizable: He is a subclass of Serializable interface, sometimes we do not want to serialize so much, can use this interface, the interface of Writeexternal () and Readexternal ( ) method to specify which properties to serialize;
But if you only want to hide a property, such as user object password pwd, if you use Externalizable, and each attribute except the PWD is written in the Writeexternal () method, it is troublesome to use the Serializable interface , and add transient in front of the attribute pwd to be hidden.
Methods of implementing serialization for Java classes