Java study notes-serializable serialization and generation of serialVersionUID settings

When looking at the code, I found that many objects developed and written have inherited a base class, which finally implements the Serializable interface.

What the hell is this?

 

What exactly is serialization?

I went to Baidu. According to my own understanding, serialization is actually a kind of "stream operation" of our java objects. This operation makes our objects more convenient to transmit.

Deserialization is to transform the "flow" into an object, which is a reverse process.

 

How to use it?

All objects (that is, classes) implement the Serializable interface

This kind of serialization is generally used when making remote calls .

 

Test code:

public class Student implements Serializable {

    private static final long serialVersionUID = 8375790720331916965L;
    
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Note: If some fields do not need to be serialized, then use transient to modify the fields that do not need to be serialized

In addition, under normal circumstances, as mentioned earlier, the base class is inherited, and the base class then implements the serialization interface.

 

How does IDEA automatically generate settings for serialVersionUID

Article link: https://blog.csdn.net/hetongun/article/details/81904393 (Pro test is available, I am a mac computer, the shortcut keys in the article, mac with brain can be replaced by option+enter)

 

Guess you like

Origin blog.csdn.net/mumuwang1234/article/details/112801791