Popular understanding of Java serialization

Source: Internet
Author: User

1 What does serialization do?

Moving.

This is simply to preserve the state of 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 that should be better than your own to preserve the state of objects, which is serialization.

Like moving, can only carry some small things, can not move a room, save the instance variable

2 When is it going to work?

When do I need to move?

A) When you want to save the state of an object in memory in a file or in a database;
b) When you want to use sockets to transfer objects on the network;
c) When you want to transfer objects through RMI;

Moving, it's just a move.

3 What happens when you serialize an object?

Before serialization, each object that is saved in the heap has a corresponding state, the instance variable (instance ariable), such as:

Java code
Foo  myfoo = new Foo ();  Myfoo. SetWidth (PNS);  Myfoo.setheight (+);  

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.

Java code
FileOutputStream fs = new FileOutputStream ("Foo.ser");  ObjectOutputStream OS = new ObjectOutputStream (fs);  Os.writeobject (Myfoo);  

4. Steps for serializing (saving to a file)
a) Make a fileoutputstream: things on the car

Java Code
    1. FileOutputStream fs = new FileOutputStream ("Foo.ser");    

        

b) Make a objectoutputstream: handover

Java code
    1. ObjectOutputStream OS =  new ObjectOutputStream (FS);  

        

c) Write the object: get off the thing

Java code
    1. Os.writeobject (MYOBJECT1);  Os.writeobject (MYOBJECT2);  Os.writeobject (MYOBJECT3);  

        

d) Close the ObjectOutputStream: end of Move

Java code
Os.close ();  

5. Examples and explanations

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, the related matters needing attention
A) When serializing, only the state of the object is saved, regardless of the object's method;
b) When a parent class is serialized and the subclass is automatically serialized, no explicit implementation of the serializable interface is required;
c) When an instance variable of an object refers to another object, the object is serialized as well.
D) Not all objects can be serialized, and there are a number of reasons why not, 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., during the serialization of the transfer process, the object's private domain 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 there is no need to implement this.

 

Popular understanding of Java serialization

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.