[What does serialization in]java mean? What's the benefit?

Source: Internet
Author: User
Tags object serialization

1. What is serialization? This is simply to preserve the state of the various objects in memory and to read the state of the saved objects again. Although you can save object states in a variety of ways, Java provides you with a mechanism that should be better than your own to preserve the state of objects, which is serialization. 2, under what circumstances need to serialize a) when you want to save the memory of the object in a file or database, B) when you want to use a socket on the network to transfer objects, C) when you want to transfer objects through RMI, 3, when the implementation of an object serialization, what happened? Before serialization, each object that is stored in the heap has a corresponding state, the instance variable (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 (37,70) in the Myfoo object are saved to the Foo.ser file, so that it can be read from the file later and the original object is recreated in the heap. Of course, saving is not only the value of the instance variable of the object, but the JVM also holds some small amount of information, such as the class type, to restore the original object. FileOutputStream fs = new FileOutputStream ("Foo.ser"); ObjectOutputStream OS = new ObjectOutputStream (FS); Os.writeobject (Myfoo); 4, the implementation of serialization (save to a file) step 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 Objectjava code os.writeobject (MYOBJECT1); Os.writeobject (MYOBJECT2); Os.writeobject (MYOBJECT3); d) Close the Objectoutputstreamjava Code os.close (); 5. Illustrate JavaCode import java.io.*; public class Box implements Serializable {private int width; private int height, public void setwidth (int width) {this.wi DTH = 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, related considerations a) when a parent class implements serialization, the subclass automatically implements the serialization, does not need to implement the Serializable interface explicitly, B) when an instance variable of an object references another object, the object is serialized, and the reference object is serialized; c) Not all objects can be serialized, As for why not, there are a number of reasons, such as: 1. Security reasons, such as an object has Private,public and other fields, for a transmission of the object, such as writing to a file, or RMI transmission, etc., in the process of serialization for transmission,  The private domain of this object is not protected. 2. Resource allocation reasons, such as the Socket,thread class, can be serialized, transmitted or saved, and cannot be re-allocated, and it is not necessary to implement
Turn an object into a byte sequence for easy transmission.
Just like you send a box of biscuits, because the volume is too large, it is all pressed into a powder tightly sent out, this is the role of serialization. However, the serialization of Java can be completely restored.
The so-called serialization is actually the data (objects) in the program in some way, saved to the local. Then the process of converting a Java object to a sequence of bytes is called the serialization of the object; There are two main kinds of serialization of objects

[What does serialization in]java mean? What's the benefit?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.