Why do entity classes in Java implements Serializable?

1. Serialization and deserialization

First, let's explain what serialization and deserialization are:

Serialization: The process of converting an object into a sequence of bytes is called serialization of an object.
Deserialization: The process of restoring a sequence of bytes to an object is called deserialization of an object.

  • When Java communicates with other languages, it is necessary to convert objects into a common format such as Json (converted into objects that everyone knows). The conversion process from objects to Json strings is the process of serialization. In turn, from The process of converting Json strings into Java objects is the process of deserialization.
  • When Java needs to save the state of an object to a file or database, it is an intermediate process in the process of data access, and serialization is required. We can understand the serialization process as "freeze", which freezes a Java object and then stores it; when it is needed again, it can be used by "de-freeze".

2. Why implements Serialzable?

  • A class can only be serialized if it implements the Serializable interface. Therefore, if objects of certain classes are to be serialized, these classes must implement the Serializable interface. In fact, the source code of Serializable is an empty interface without any specific content. Its purpose is simply to indicate that an object of a class can be serialized.
  • serialization allows you to convert an object that implements the Serializable interface into a sequence of bytes that can be fully stored for later regeneration of the original object.

3. So when do you need to implement serialization?

  • Write the object in memory to the hard disk. For example, if your memory is not enough, the computer will temporarily save some objects in the memory to the hard disk, and then read them into the memory when it is needed. The storage space of the hard disk is the so-called virtual memory. For another example, if you want to save a specific object to a file and use it every few days, then you need to implement the Serializable interface at this time.
  • Use sockets to transmit objects on the network. Usually, you can use the outputstream inputstream of the socket on the server side to process the obtained data into strings and send them to the client, and then split the strings on the client side, but this is obviously It will reduce the efficiency, so you can wrap the server-side data into a class implements Serializable, and then directly pass it through objectoutputstream and objectinputstream.
  • Objects are transferred through Java's RIM (remote method invocation). RMI allows objects on this machine to operate on objects on remote machines the same way. When sending a message to a remote object, you need to use the serializaiton mechanism to send parameters and receive return values.

Entity classes implement the purpose of serialization:

  • One is easy to store
  • Second, it is easy to transport

Types like boolean, int, and long are all basic data types, and there are corresponding data structures in the database. From the perspective of the class declaration, we thought that serialization was not performed. In fact, when declaring various variables, the specific data type helped us realize the serialization operation. So even if we don't implement serializable, it can still operate normally.

If you usually pay attention, we will find that when the serialization operation is used for storage, it is generally for  NoSql  database, and when using Nosql database for storage, such as redis, it does not have data structures such as varchar and int. And in the absence of it, we really need to store it, then we need to serialize the object.

4. Why do you want to display the declaration serialVersionUID

The role of serialVersionUID is to verify whether the object is consistent during serialization and deserialization. So in general we need to declare the serialVersionUID explicitly. If the serialVersionUID of the class loaded by the receiver is different from the class version number of the sender, the deserialization will throw an InvalidClassException error.

When will this error be reported? For example, when the declared version number is not displayed, the object is serialized first; then no matter what the purpose is, the class of the object is modified, even if only a mandatory conversion is added, or a public property is changed to private If it is changed, it will affect the version number. At this time, in this environment, using the deserialization method to read the previously serialized and stored objects will result in an error.

@Data
public class Student implements Serializable {
 
    private static final long serialVersionUID = 1L;
 
    @TableId("id")
    private Integer id;
 
    @TableField("name")
    private String name;
 
    @TableField("age")
    private Integer age;
}

Guess you like

Origin blog.csdn.net/weixin_70280523/article/details/131316379