Serialization and deserialization practices for Java objects

Source: Internet
Author: User

When two processes are communicating remotely, they can send different types of data. Regardless of the type of data, it is transmitted over the network in the form of a binary sequence. The sender needs to convert the Java object into a byte sequence to be routed over the network, and the receiver needs to revert the byte sequence back to the Java object.

The process of converting Java objects into byte sequences is called serialization of objects.

The process of restoring a byte sequence to a Java object is called deserialization of an object.

There are two main uses for serialization of objects:

1 The object's byte sequence is permanently saved to the hard disk, usually stored in a file;

2 transfer the byte sequence of the object on the network.

A Serialization APIs in the JDK class library

Java.io.ObjectOutputStream represents an object output stream whose WriteObject (object obj) method serializes the Obj object specified by the parameter and writes the resulting byte sequence to a target output stream.

Java.io.ObjectInputStream represents an object input stream whose ReadObject () method reads a sequence of bytes from a source input stream, deserializes them into an object, and returns them. 、

Only objects of classes that implement the serializable and Externalizable interfaces can be serialized. The Externalizable interface inherits from the serializable interface, and the class that implements the Externalizable interface controls the serialization behavior entirely by itself, and the class that implements only the serializable interface can use the default serialization method.

Object serialization consists of the following steps:

1 Create an object output stream that can wrap a different type of target output stream, such as a file output stream;

2 The object is written through the WriteObject () method of the object output stream.

The steps to deserialize an object are as follows:

1 Create an object input stream that can wrap a different type of source input stream, such as a file input stream;

2 The object is read by the ReadObject () method of the object input stream.

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.