Methods of implementing serialization for Java classes

Source: Internet
Author: User
Two ways to implement serialization in Java Serializable interface and externalizable interface

The serialization of an object is the writing of an object to the output stream.

Deserialization is the reading of an object from an input stream.

The classes used to implement serialization are in the Java.io package, and the classes or interfaces we use are:

ObjectOutputStream: Provides a way to serialize an object and write it to the stream

ObjectInputStream: Reading the stream and deserializing the object

Serializable: An object wants to be serialized, then its class implements this interface, and all properties of the object (including the private property, including its reference object) can be serialized and deserialized to save, pass.

externalizable: He is a subclass of Serializable interface, sometimes we do not want to serialize so much, can use this interface, the interface of Writeexternal () and Readexternal ( ) method to specify which properties to serialize;

But if you only want to hide a property, such as user object password pwd, if you use Externalizable, and each attribute except the PWD is written in the Writeexternal () method, it is troublesome to use the Serializable interface , and add transient in front of the attribute pwd to be hidden.

Methods of implementing serialization for Java classes

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.