What does serialization mean in Java? What are the benefits?

Transferred from:

 1、 What does serialization do?

In short, it is to save the states of various objects in memory, and the saved object states can be read out again. Although you can use your own various methods to save object states, Java provides you with a better mechanism to save object states than yourself, that is < a class = "inner link Decor none" href=“%20http://zhidao.baidu.com/search?word=%E5%BA%8F%E5%88%97%E5%8C%96&%20;%20fr=qb_%20search_%20exp&%20ie=utf8" rel="nofollow" style="color:rgb(7,179); "> serialization.

2. Under what circumstances do you need < a class = "inner link Decor none" href=“%20http://zhidao.baidu.com/search?word=%E5%BA%8F%E5%88%97%E5%8C%96&%20;%20fr=qb_%20search_%20exp&%20ie=utf8" rel="nofollow" style="color:rgb(7,179); "> serialization

a) When you want to save objects in memory to a file or database; b) When you want to use < a class = "inner link Decor none" href=“ http://zhidao.baidu.com/search?word=%E5%A5%97%E6%8E%A5%E5%AD%97& ; fr=qb_ search_ exp& ie=utf8" rel="nofollow" style="color:rgb(7,179); "> when the socket transmits objects on the network; c) when you want to transmit objects through RMI;

3. What happens when serializing an object?

Before serialization, each object saved in the heap has a corresponding state, that is, < a class = "inner link Decor none" href=“ http://zhidao.baidu.com/search?word=%E5%AE%9E%E4%BE%8B%E5%8F%98%E9%87%8F& ; fr=qb_ search_ exp& ie=utf8" rel="nofollow" style="color:rgb(7,179); "> instance variable, such as:

Foo myFoo = new Foo(); myFoo . setWidth(37); myFoo. setHeight(70);

When serialized through the following code, the width and height in the myfoo object < a class = "inner link Decor none" href=“ http://zhidao.baidu.com/search?word=%E5%AE%9E%E4%BE%8B%E5%8F%98%E9%87%8F& ; fr=qb_ search_ exp& ie=utf8" rel="nofollow" style="color:rgb(7,179); "> the values (37, 70) of the instance variables are saved in the foo.ser file, so that you can read them from the file later and re create the original object in the heap. Of course, saving is not just about saving the < a class =" inner link Decor none "href =" http://zhidao.baidu.com/search?word=%E5%AE%9E%E4%BE%8B%E5%8F%98%E9%87%8F& ; fr=qb_ search_ exp& ie=utf8" rel="nofollow" style="color:rgb(7,179); "> the value of the instance variable. The JVM also needs to save a small amount of information, such as class type, in order to restore the original object.

FileOutputStream fs = new FileOutputStream("foo.ser"); ObjectOutputStream os = new ObjectOutputStream(fs); os. writeObject(myFoo);

4. Steps for serialization (saving to a file)

a) Make a fileoutputstream java code fileoutputstream FS = new fileoutputstream ("foo. Ser"); b)Make a 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. Examples

Java code import Java io.*;

public class @R_ 993_ 2419@ 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){ @R_993_2419@ my@R_993_2419 @ = new @R_ 993_ 2419@(); my@R_993_2419 @.setWidth(50); my@R_993_2419 @.setHeight(30);

try{ FileOutputStream fs = new FileOutputStream("foo.ser"); ObjectOutputStream os = new ObjectOutputStream(fs); os.writeObject( my@R_993_2419 @); os. close(); } catch(Exception ex){ ex.printStackTrace(); } }

}

6. Relevant precautions

a) When a parent class implements serialization, the child class automatically implements serialization without explicit implementation of < a class = "inner link Decor none" href=“ http://zhidao.baidu.com/search?word=Serializable%E6%8E%A5%E5%8F%A3& ; fr=qb_ search_ exp& ie=utf8" rel="nofollow" style="color:rgb(7,179); "> serializable interface; b) when the instance variable of an object references other objects, the reference object is also serialized when the object is serialized; c) not all objects can be serialized. As for why not, there are many reasons, such as:

1. For security reasons, for example, an object has private, public and other fields. For an object to be transmitted, such as writing to a file or RMI transmission, the private and other fields of the object are not protected during serialization and transmission. 2. For resource allocation reasons, such as socket and thread classes, if they can be serialized, transmitted or saved, they cannot be reallocated. Moreover, it is not necessary to implement < pre class = "recommend text mb-10" style = "font size: 13px;" >< pre class="recommend-text mb-10" style="font-size:13px;"> Completely convert an object into a byte sequence to facilitate transmission< pre class="answer-text mb-10" style="font-size:13px;"> Just like when you send a box of biscuits, because the volume is too large, they are all pressed into powder and sent out tightly in a package. This is the function of serialization. However, Java serialization can be completely restored< pre class="answer-text mb-10" style="font-size:13px;"> The so-called serialization is actually to save the data (objects) in the program to the local in some way. Then the process of converting Java objects into byte sequences is called object serialization; There are two main types of object serialization

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>