What does serialization mean in Java? What's the benefit? __serializable interface

Source: Internet
Author: User
Tags serialization thread class
1, the serialization is what. This is simply to save the state of the various objects in memory, 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 to serialize a when you want to save the object in memory in a file or in a database; (b) When you want to use sockets to transfer objects over the network; c when you want to transfer objects via RMI; 3, what happens when you serialize an object. 
Before serialization, each object saved in the heap (HEAP) has a corresponding state, i.e. an instance variable (instance ariable) such as: 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. 
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, examples of 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, related considerations a) when a parent class implements serialization, subclasses are automatically serialized without the need to explicitly implement the Serializable interface; b When an object's instance variable references another object, serializing the object also serializes the referenced object; c) 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 RMI transmission, etc., in the process of serialization,
  The private domain of this object 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 their resources, and there is no need to achieve this
Turn an object completely into a byte sequence for easy transmission.
It's like you send a box of cookies, because the volume is too large, it's all pressed into powder tightly to the packet sent out, this is the role of serialization.
only Java serialization can be completely restored.
Serialization is a way of saving data (objects) in a program to the local. The process of converting a Java object into a byte sequence is called the serialization of objects; There are two main types of serialization of objects

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.