In general, we inherit the Serializable interface when defining an entity class, like this:

We refer to the Serializable interface in the entity class. What does this interface have? If you’re careful, you’ll notice that we’ve also defined a serialVersionUID variable. What exactly does this variable do?

What is the Serializable interface

An interface for object serialization. A class can only serialize its objects if it implements the Serializable interface.

What is serialization?

Serialization is the process of converting the state of an object into a format that can be retained or transferred. The opposite of serialization is deserialization, which converts a stream to an object. Together, these two processes make it easy to store and transfer data.

Why serialize objects

The process of converting an object into a sequence of bytes is called serialization of an object the process of converting a sequence of bytes back into an object is called deserialization of an object when is serialization required?

When we need to transmit the state information of the object over the network, or need to persist the state information of the object for future use, we need to serialize the object

So why inherit Serializable? That is to store objects in a storage medium so that a copy can be quickly rebuilt the next time it is used.

You may ask, in my development process, the entity did not implement serialization, but I can also save data to mysql, Oracle database, why must serialization to store?

What is Serializable? There is nothing in the Serializable interface, just an empty interface

An interface that has nothing in it can be thought of as an identity interface.

For example, when a student meets a problem in class, he raises his hand to ask the teacher for advice, and the teacher helps him to answer it. Then the student’s hand is actually a sign that he can’t solve the problem by himself and asks the teacher for help. The Serializable interface in Java is actually for the JVM to see, I don’t serialize this class, you serialize it for me.

Serializable interface is a mechanism Java provides for efficient remote sharing of instance objects.

What is the JVM?

The JVM, short for Java Virtual Machine, is a specification for computing devices. The JVM is an imaginary computer that is implemented by emulating various computer functions on an actual computer.

Why define the serialversionUID variable

Take a look at the description of the Serializable interface

As you can see from the instructions, if we do not declare a serialVersionUID variable ourselves, the interface will generate a serialVersionUID by default

However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpectedInvalidClassExceptions during deserialization.

However, it is highly recommended that you customize a serialVersionUID, as the default serialVersinUID is sensitive to class details and may result in an InvalidClassException when deserialized.

Previously we created an entity class User that implements the Serializable interface and defines the serialVersionUID variable.

We write the User to the file and then read it out.

Serialization and deserialization operations simply write the User to a file and then restore the User from the file. The restored content is exactly the same as before, but they are different objects. As mentioned earlier, let’s see what happens if we drop the serialVersionUID variable.

I mentioned earlier, should I specify the serialVersionUID? What happens if you do not specify it? What does it mean if you specify after and after values? Since the system specified this field, it must have a purpose.

This serialVersionUID is used to assist the serialization and deserialization of the object. In principle, the serialVersionUID in the serialized data is the same as the serialVersionUID in the current class, so that the object can be deserialized successfully. The detailed workings of the serialVersionUID are as follows: During serialization, the system will write the serialVersionUID into the serialized file. During deserialization, the system will first check whether the serialVersionUID in the file is consistent with the serialVersionUID of the current file. If the deserialization succeeds, If the number of member variables is changed or the type of member variables is changed, the current class will crash, and the error will be returned:

java.io.InvalidClassException: User; local class incompatible: stream classdesc serialVersionUID = -1451587475819212328, local class serialVersionUID = -3946714849072033140at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:699)at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1885)at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1751)at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2042)at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1573)at java.io.ObjectInputStream.readObject(ObjectInputStream.java:431)at Main.readUser(Main.java:32)at Main.main(Main.java:10)