In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
How to parse the principle of serialization and deserialization of java objects? I believe many inexperienced people don't know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
This article mainly introduces the principle analysis of java object serialization and deserialization, which is introduced in great detail through the sample code, which has a certain reference value for everyone's study or work. Friends who need it can refer to it.
I. the concept of serialization and deserialization
The process of converting an object into a sequence of bytes is called object serialization. The process of restoring a sequence of bytes to an object is called deserialization of the object.
II. The role of serialization and deserialization
Object serialization has two main uses:
The byte sequence of objects is permanently saved to the hard disk, usually in a file. The byte sequence of objects transmitted over the network. What is transmitted on the network is a binary sequence.
In many applications, some objects need to be serialized to leave memory space and save to a physical hard disk for long-term preservation. For example, the most common is the Session object in the Web server. When 100000 users visit concurrently, 100000 Session objects may appear, and memory may be too much to bear, so the Web container will serialize some seesion to the hard disk first, and then restore the objects saved in the hard disk to memory after being used.
When two processes are communicating remotely, they can send various types of data to each other. No matter what type of data it is, it will be transmitted over the network in the form of a binary sequence.
The sender needs to convert the Java object into a byte sequence before it can be transmitted over the network; the receiver needs to restore the byte sequence to the Java object.
3. Serialized API in JDK class library
Java.io.ObjectOutputStream represents the object output stream, and its writeObject (Object obj) method serializes the specified obj object and writes the resulting byte sequence to a target output stream.
Java.io.ObjectInputStream represents an object input stream, and its readObject () method reads sequences of bytes from a source input stream, deserializes them into an object, and returns them.
Only objects of classes that implement the Serializable and Externalizable interfaces can be serialized. The Externalizable interface inherits from the Serializable interface, and the class that implements the Externalizable interface controls the serialization behavior entirely by itself, while the class that only implements the Serializable interface can use the default serialization mode.
Object serialization includes the following steps:
1) create an object output stream that can wrap another type of target output stream, such as a file output stream
2) write the object through the writeObject () method of the object output stream.
The steps for object deserialization are as follows:
1) create an object input stream that can wrap another type of source input stream, such as a file input stream
2) read the object through the readObject () method of the object input stream.
IV. The role of serialVersionUID
SerialVersionUID: literally a serialized version number, any class that implements the Serializable interface has a static variable that represents the serialized version identifier. A class that implements the Serializable interface reports a warning if no serialVersionUID is added to the class.
There are two ways to generate serialVersionUID:
The serialVersionUID generated by img is 1L, for example:
1 private static final long serialVersionUID = 1L
SerialVersionUID generated by img is generated based on class name, interface name, method, property, and so on, for example:
1 private static final long serialVersionUID = 4603642343377807741L
The function of serialVersionUID, a serialized version, is to be compatible with the previous version when modified, because the serialVersionUID before and after modification is the same. If the serialization version number before and after modification is different, deserialization will fail. That is to say, when we specify the serialVersionUID ourselves, we can guarantee that the number is unique among so many classes. In this way, after serialization, we can modify the previous class, such as adding a property field, or add a method without affecting the later restore, the restored object can still be used, and more methods or properties can be used.
5. The value of serialVersionUID
The value of serialVersionUID is automatically generated by the Java runtime environment based on the internal details of the class. If the source code of the class is modified and then recompiled, the value of the serialVersionUID of the newly generated class file may also change.
The default value of the serialVersionUID of a class depends entirely on the implementation of the Java compiler. For the same class, compiling with different Java compilers may result in different serialVersionUID or the same. To improve the independence and certainty of serialVersionUID, it is strongly recommended that the definition serialVersionUID shown in a serializable class be given an explicit value.
There are two uses for explicitly defining serialVersionUID:
In some cases, you want different versions of the class to be compatible with serialization, so you need to make sure that different versions of the class have the same serialVersionUID
In some cases, you don't want different versions of the class to be compatible with serialization, so you need to make sure that different versions of the class have different serialVersionUID.
VI. Summary
Serialization is the serialization of objects to facilitate storage and transmission over the network.
It is important to note, however, that serialization saves the state of the object, and static variables belong to the state of the class, so serialization does not save static variables.
If you don't want a variable to be serialized, you can prefix the variable with the Transient keyword.
The function of the Transient keyword is to control the serialization of the variable. Adding this keyword before the variable declaration can prevent the variable from being serialized into the file. After being deserialized, the value of the transient variable is set to the initial value, such as 0 for int and null for the object.
After reading the above, have you learned how to parse the principles of serialization and deserialization of java objects? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.