The understanding of Java Serializable (serialization)

Source: Internet
Author: User
1, the serialization is what.
This is simply to save the state of the various objects in memory (i.e. instance variables, not methods) and to read the saved object state again. Although you can save object states in a variety of ways, Java provides you with a mechanism to preserve the state of objects better than yourself, and that is serialization.

2, under what circumstances need serialization
(a) When you want to save the state of an object in memory into a file or database;
b When you want to use sockets to transfer objects on the network;
c When you want to transfer the object via RMI;

3. What happens when you serialize an object.
Before serialization, each object saved in the heap (HEAP) has a corresponding state, that is, an instance variable (instance ariable), such as:
Java code foo myfoo = new Foo ();   Myfoo. SetWidth (37);          Myfoo.setheight (70); After serialization through the following code, the value of the width and height instance variables (37,70) in the Myfoo object are saved to the Foo.ser file, so that it can later be read from the file and recreated in the heap to create the original object. Of course the save time is not only to save the value of an instance variable of an object, but also to save a small amount of information, such as the type of class, to restore the original object.
Java code FileOutputStream FS = new FileOutputStream ("Foo.ser");   ObjectOutputStream OS = new ObjectOutputStream (FS); Os.writeobject (Myfoo);


4, to achieve 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 object
Java code os.writeobject (MYOBJECT1);   Os.writeobject (MYOBJECT2);       Os.writeobject (MYOBJECT3); d) Close the ObjectOutputStream
Java code os.close ();


5, the example explains Java Code Importjava.io.*;
Public classBoxImplementsSerializable {       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 (;  )             try{                fileoutputstream fs = new fileoutputstream ("Foo.ser");                ObjectOutputStream os =  new  ObjectOutputStream (FS);                Os.writeobject (mybox);                Os.close ();           }catch (Exception ex) {               ex.printstacktrace ();           }       }          }  

6, related matters needing attention
A when serialized, only the state of the object is saved, regardless of the object's method;
b When a parent class is serialized, the subclass is automatically serialized and the serializable interface is not explicitly implemented;
C When an object's instance variable refers to another object, serialization of the object also serializes the referenced object;
D Not all objects can be serialized, as for why not, there are many reasons, such as:
1. Security reasons, such as an object with Private,public field, for a transmission of objects, such as writing to a file, or for RMI transmission, etc., in the process of serialization, the object's private domain is not protected.
2. Resource allocation reasons, such as the Socket,thread class, if they can be serialized, transmitted or saved, cannot be reallocated to them, and it is not necessary to achieve this.

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.