Serialization of Java Objects

Source: Internet
Author: User
Tags object serialization

Defined:

The object serialization goal is to save the object on disk, allowing the object to be transferred directly in the network. The serialization mechanism transforms the in-memory Java object into a platform-independent binary stream, which allows the binary

The stream is permanently saved on disk, and the binary stream is sent to another network node through the network, and once the other program obtains the binary flow, it can be converted to a Java object, whether it is obtained from the disk or the network.

If you want to serialize an object, this object must implement the Serializable interface and implement this interface without implementing any method, which simply tells Java that this object can be serialized.

This interface is a markup interface.

The user object that is serialized,

Importjava.io.Serializable; Public classUserImplementsserializable{PrivateString name; Private intAge ;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }}

Serializes the user object to create an output stream output to disk

 Public Static void throws exception{        new fileoutputstream ("D:/object.txt");         =new  objectoutputstream (FOS);                 =new  User ();        U.setname ("Jack");        U.setage (+);                Oos.writeobject (u);    }

deserialization, creating an input stream, reading from disk

 Public Static void throws exception{        new fileinputstream ("D:/object.txt");         =new  objectinputstream (FIS);                 =(User) ois.readobject ();         // output jack            System.out.println (U.getname () + "\ T" +U.getage ());    }

As a result, serialization is easy to understand.

There are a few other notes:

1. Deserialization does not initialize the Java object through the constructor, and it can be validated by a construction method.

2. If serialization writes multiple objects to a file, deserialization must be read in the order in which they are actually written.

3. If the direct or indirect parent class of the serialized object is not serializable, only with a parameterless constructor, the member defined by the parent class is not serialized.

4. The serialized reference class must also implement the Serializable interface;

Object reference Serialization:

The serialized object person has a reference to a user object:

Importjava.io.Serializable; Public classPersonImplementsserializable{PrivateString ID; PrivateUser u;  PublicString getId () {returnID; }     Public voidsetId (String id) { This. ID =ID; }     PublicUser Getu () {returnu; }     Public voidSetU (User u) { This. u =u; }}

 If an object is referenced by several other different objects and needs to be serialized, Java does not serialize the consent object multiple times, so there is a special algorithm for the Java sequence (the serialization of the underlying mechanism):

* All serialized objects saved to disk have a number.

* When the program attempts to serialize an object, Java checks to see if the object has been serialized in this virtual machine first.

* If serialized, outputs the last serialized number instead of serializing the object.

So, do a repeat serialization test

     Public Static voidMain (string[] args) {Try{FileOutputStream fos=NewFileOutputStream ("D:/object.txt"); ObjectOutputStream Oos=NewObjectOutputStream (FOS); FileInputStream FIS=NewFileInputStream ("D:/object.txt"); ObjectInputStream Ois=NewObjectInputStream (FIS); User u=NewUser (); U.setname ("First time serialization");            Oos.writeobject (U); U.setname ("Second serialization");            Oos.writeobject (U); //Read StreamOis.readobject (); User UI=(User) ois.readobject (); //Discovery Output: First time serializationSystem.out.println (Ui.getname ()); }Catch(Exception e) {e.printstacktrace (); }    }

The mechanism that found the result to match it did not do two serialization, even if the object's properties were modified .

Serialization of Java 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.