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

Example Analysis of serialization and deserialization of Java object

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

Share

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

This article introduces the relevant knowledge of "Java object serialization and deserialization example analysis". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. What is serialization and deserialization?

Serialization: refers to storing Java object data in heap memory in some way to disk files or passing them to other network nodes (transfer over the network). This process is called serialization. Generally speaking, it is the process of converting a data structure or object into a binary string.

Deserialization: the process of restoring object data in a disk file or on a network node to an Java object model. That is, the process of converting a binary string generated during serialization into a data structure or object.

2. Why serialize?

①, in distributed systems, when you need to transfer objects on the network, you have to convert object data into binary form, and JavaBean objects that need to be shared have to be serialized.

②, server passivation: if the server finds that some objects have not been active for a long time, then the server will persist these memory objects in the local disk file (Java objects into binary files); if the server finds some objects that need to be active, first look for them in memory, and then go to the disk file to deserialize our object data and restore it to Java objects. This saves server memory.

3. How to serialize Java?

①, classes that need to serialize objects, must implement the serialization interface: the Java.lang.Serializable interface (this is a flag interface without any abstract methods), which is implemented by most classes in Java, such as String,Integer

②, the underlying layer will judge that if the current object is an instance of Serializable, serialization is allowed, and the Java object instanceof Serializable is allowed to judge.

③, using object streams in Java to serialize and deserialize

ObjectOutputStream: serialize through the writeObject () method

ObjectInputStream: deserialization through the readObject () method

Step 1: create a JavaBean object

Step 2: serialize using the ObjectOutputStream object

We open the a.txt file and find that the contents are garbled. Note that we don't need to understand this. This is a binary file that the computer can read.

Error 1: if the newly created Person object does not implement the Serializable interface, the above operation will report an error:

Step 3: use ObjectInputStream objects to deserialize

The deserialized object must provide the bytecode file of the object. class

Question1: what if some data does not need to be serialized, such as passwords, such as the age above?

Solution: add transient in front of the field

So when we deserialize, what we print out is Person [name=vae, age=0], and the default value of integer data is 0.

Question 2: serialization version problem. After the serialization operation is completed, due to the upgrade or modification of the project, we may modify the serialization object, such as adding a field, then we will report an error in deserialization:

Solution: add a serialVersionUID field to the JavaBean object to fix this version. No matter how we modify it, the version is consistent and can be deserialized.

/ * *

* serialize ID

, /

Private static final long serialVersionUID = 8656128222714547171L

This is the end of the content of "serialization and deserialization example Analysis of Java objects". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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