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 is object serialization for and what is its function in Java

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

I believe many inexperienced people are at a loss about what object serialization is for and what it does in Java. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

1. What is serialization for? To put it simply, it is to save the state of various objects in memory, and the saved object state can be read out again. Although you can save Object States in a variety of ways of your own, Java provides you with a better mechanism for saving object state than you do, and that is serialization.

2. When do you need serialization?

A) when you want to save the objects in memory to a file or database

B) when you want to send objects over the network using sockets

C) when you want to transfer objects through RMI

3. What happens when you serialize an object?

Before serialization, every object stored in the Heap has a corresponding state (state), that is, instance variables (instance ariable), such as:

Foo myFoo = new Foo ()

MyFoo .setWidth (37)

MyFoo.setHeight (70)

When serialized by the following code, the values of the width and Height instance variables in the MyFoo object (37570) are saved to the foo.ser file so that it can be read out of the file later and recreate the original object in the heap. Of course, when saving, JVM not only saves the value of the instance variable of the object, but also stores some small amount of information, such as the type of the class, in order to restore the original object.

FileOutputStream fs = new FileOutputStream ("foo.ser")

ObjectOutputStream os = new ObjectOutputStream (fs)

Os.writeObject (myFoo)

4. Steps to serialize (save to a file)

A) Make a FileOutputStream

Java code

FileOutputStream fs = new FileOutputStream ("foo.ser")

B) Make an ObjectOutputStream

Java code

ObjectOutputStream os = new ObjectOutputStream (fs)

C) write the object

Java code

Os.writeObject (myObject1)

Os.writeObject (myObject2)

Os.writeObject (myObject3)

D) close the ObjectOutputStream

Java code

Os.close ()

5. Give an example

Java code

Import java.io.*

Public class Box implements Serializable

{

Private int width

Private int height

Public void setWidth (int width) {

This.width = width

}

Public void setHeight (int height) {

This.height = height

}

Public static void main (String [] args) {

Box myBox = new Box ()

MyBox.setWidth (50)

MyBox.setHeight (30)

Try {

FileOutputStream fs = new FileOutputStream ("foo.ser")

ObjectOutputStream os = new ObjectOutputStream (fs)

Os.writeObject (myBox)

Os.close ()

} catch (Exception ex) {

Ex.printStackTrace ()

}

}

}

6. Matters needing attention

A) when a parent class implements serialization, the subclass automatically serializes, without the need to explicitly implement the Serializable interface

B) when the instance variable of an object refers to another object, the reference object is also serialized when the object is serialized

C) not all objects can be serialized, but there are many reasons why not, such as:

1. For security reasons, for example, an object has field such as private,public, for an object to be transferred, such as writing to a file, or rmi transfer, etc., in the process of serialization for transfer, the object's private and other fields are not protected.

two。 Resource allocation reasons, such as socket,thread classes, cannot be reallocated if they can be serialized, transferred, or saved, and it is not necessary to do so.

After reading the above, have you mastered what object serialization is and what it does in Java? 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.

Share To

Development

Wechat

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

12
Report