Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the functions of serializable interface

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you what the role of the serializable interface, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

The function of the serializable interface: 1, the storage object is in the storage medium, so that a copy can be quickly rebuilt the next time it is used; 2, it is convenient for data transmission, especially when calling remotely.

The purpose of the serializable interface:

The 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 serialize or deserialize their arbitrary state.

Personal understanding:

This introduction is very unapproachable, so I went to read several blogs. There was a little sister's blog that caught my attention. She understood it like this:

The process of serialization is a "freeze" process that holds an object freeze (frozen), stores it, and then de-freeze the object for immediate use when needed again.

We thought there was no serialization, but in fact, when declaring different variables, the specific data type helped us to implement the serialization operation.

If someone has opened the source code of the Serializable interface, they will find that the interface is actually an empty interface, so who implements the serialization operation? In fact, if you look at the comments on the interface, you can see that when we ask the entity class to implement the Serializable interface, we are telling JVM that the class can be serialized and can be serialized by the default serialization mechanism.

The purpose of serialization:

* Serialization is the process of converting the state of an object to a format that can be maintained or transmitted. The opposite of serialization is deserialization, which converts the stream into an object. Combined with these two processes, data can be easily stored and transferred.

1. The storage object is 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 do not implement serialization, I can still store it in my sqlserver or MySQL or Oracle database. Why do I have to serialize to store it?

2, to facilitate data transmission, especially when calling remotely

The interface java.io.Serializable is implemented for serialization, and serialVersionUID is used to indicate compatibility between different versions of the serialized class. If you modify this class, change this value.

Otherwise, there will be an error when restoring a class that was previously serialized with an older version of the class.

After implementation, if you use a tool, it will warn you that you can automatically generate private static final long serialVersionUID = 1L; in order to ensure the compatibility of the class version when deserialization, it is best to add the private static final long serialVersionUID attribute to each class to be serialized, and the specific value is defined by yourself.

The explanation of serialVersionUID

The role of serialVersionUID: serialization in order to maintain version compatibility, that is, deserialization still maintains the uniqueness of the object when the version is upgraded.

You can write any one, and it will generate one for you in Eclipse. There are two ways to generate it: one is 1L by default, such as:

Private static final long serialVersionUID = 1L

One is to generate a 64-bit hash field based on class name, interface name, member methods and properties, such as:

Private static final long serialVersionUID =-8940196742313994740L; above is all the content of the article "what is the function of serializable interface". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report