Home  >  Article  >  What is the function of serializable interface

What is the function of serializable interface

coldplay.xixi
coldplay.xixiOriginal
2021-02-22 14:33:4230805browse

The functions of the serializable interface: 1. Store the object in the storage medium so that a copy can be quickly reconstructed the next time it is used; 2. Facilitate data transmission, especially during remote calls.

What is the function of serializable interface

#The operating environment of this article: Windows 7 system, Dell G3 computer.

The role of serializable interface:

Serializable interface is the interface that enables its serialization function. Classes that implement the java.io.Serializable interface are serializable. Classes that do not implement this interface will not be able to have any of their state serialized or deserialized.

Personal understanding:

This introduction is very ungrounded, so I went to read a few blogs, and a young lady’s blog caught my attention. This is how she understood it. :

The serialization process is a "freeze" process. It freezes an object and then stores it. When it is needed again, the object can be de-freezed immediately. use.

We thought there was no serialization. In fact, when declaring different variables, the specific data types helped us realize the serialization operation.

If someone opens the source code of the Serializable interface, they will find that this interface is actually an empty interface. So who implemented this serialization operation? In fact, if you look at the annotations of the interface, you will know that when we let the entity class implement the Serializable interface, we are actually telling the JVM that this class can be serialized and can be serialized by the default serialization mechanism.

The role of serialization:

*Serialization is the process of converting object state into a format that can be maintained or transmitted. The opposite of serialization is deserialization, which converts a stream into an object. These two processes combine to easily store and transfer data.

1. Store the object in the storage medium so that a copy can be quickly rebuilt the next time it is used. That is, When the resulting series of bits is reread according to the serialization format, it can be used to create a semantically identical clone of the original object.

Question: When I did not implement serialization, I can also store it in my sqlserver or MySQL or Oracle database. Why does it have to be serialized before it can be stored? ? ? ?

2, to facilitate data transmission, especially when making remote calls

Implementing the java.io.Serializable interface is for serialization, and serialVersionUID is used to indicate the implementation sequence Compatibility between different versions of the class. If you modify this class, modify this value.

Otherwise, errors will occur when restoring classes that were previously serialized with older versions of classes.

After implementation, if you are using a tool, a warning will appear and it will prompt you. You can automatically generate private static final long serialVersionUID = 1L; In order to ensure the compatibility of class versions during deserialization, it is best to Fortunately, the private static final long serialVersionUID attribute is added to each class to be serialized, and the specific value is defined by yourself. Compatibility, that is, deserialization still maintains the uniqueness of the object when the version is upgraded.

You can write one at will, and it will generate one for you in Eclipse. There are two ways to generate it: one is the default 1L, for example:

private static final long serialVersionUID = 1L;

The other is based on the class name and interface name , member methods and attributes, etc. to generate a 64-bit hash field, such as:

private static final long serialVersionUID = -8940196742313994740L;

Related free learning recommendations:

Programming video

The above is the detailed content of What is the function of serializable interface. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn