concept
Serialization refers to converting an object intoThe process of transferable formats. It is a means of data persistence.
In the Java serialization mechanism, if it is a String, enumerated or a class that implements the Serializable interface, you can use the Java serialization mechanism toClass serialized to encoded data streams, and then persist the in-memory classes to the hard disk or network through InputStream and OutputStream.
What are the differences between Serializable and Externalizable interfaces?
All subtypes that the Serializable interface can be serializable are themselves serializable.
If the class to be serialized has a parent class and if you want to persist the variables in the parent class, the parent class should also implement the Serializable interface.
Externalizable inherits Serializable. This interface defines two methods. When using the Externalizable interface to serialize and deserialize, these two methods need to be overwritten. If no specific serialization details are defined, the serialized object is empty.
What is the use of serialVersionUID? What's wrong with not defining it?
Whether a virtual machine allows deserialization depends not only on whether the classpath and functional code are consistent, but a very important point is whether the serialization IDs of the two classes are consistent. That is, serialVersionUID is consistent.
When deserializing, the JVM will compare the serialVersionUD in the passed byte stream with the serialIVersionUID of the local corresponding entity class. If the same is considered to be consistent, it can be deserialized. Otherwise, an exception with inconsistent serialization versions will occur, that is, InvalidCastException. This is done to ensure security, as the contents in the file storage may be tampered with.
Notice: If you do not define the SerialVersionUID when using it, if the information of this class is modified after serializing the object (for example, adding a field), the desequence will fail because there will be two different SerialVersionUIDs.
class User implements Serializable{ private String name; }
If an object of the above class is defined and saved to a file (the SerialVersionUID is not specified here).
Then modify this class and add a property.
class User implements Serializable{ private String name; private int age; }
An InvalidClassException is thrown when deserialization is performed, and the two serialVersionUIDs are different.
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.