Compared with java. io. Externalizable and java. io. Serializable)

Source: Internet
Author: User

Below are the code-level associations between serialization and Externalization:

Public interface Serializable {}

Public interface Externalizable extends Serializable {
Void readExternal (ObjectInput in );
Void writeExternal (ObjectOutput out );
}

Major differences between serialization and Externalization

Externalization and serialization are two different methods to achieve the same goal. Next let's analyze the main differences between serialization and externalization.

The support for Object serialization through the Serializable interface is built in the core API, but all the implementers of java. io. Externalizable must provide read and write implementations. Java already has built-in support for serialization. That is to say, as long as you make your own java. io. Serializable class, Java will try to store and reorganize your objects. If you use externalization, you can choose to read and write data by yourself. The only support for externalization in Java is interface:

VoidreadExternal (ObjectInput in)
Void writeExternal (ObjectOutput out)

Now you can fully understand how to implement readExternal () and writeExternal.

Serialization automatically stores necessary information for deserializing the stored instance, while externalization only stores the identifier of the stored class. When you serialize an object through the java. io. Serializable interface, the information about the class, such as its attributes and the types of these attributes, are stored together with the instance data. When Externalizable is selected, Java only stores very little information about each stored type.

Advantages and disadvantages of each interface

SerializableInterface

·Advantages:Built-in support

·Advantages:Easy to implement

·Disadvantages:The occupied space is too large.

·Disadvantages:Slow speed due to additional overhead

ExternalizableInterface

·Advantages:Low overhead (the programmer decides what to store)

·Advantages:Possible Speed improvement

·Disadvantages:Virtual machines do not provide any help, that is to say, all work falls on the shoulders of developers.

The choice between the two depends on the needs of the application. Serializable is usually the simplest solution, but it may lead to unacceptable performance problems or space problems; in the case of these problems, Externalizable may be a viable path.

Remember that if a class is Externalizable, The Externalizable method will be used to serialize the instance of the class, even if the type provides the Serializable method:

Private void writeObject ()
Private void readObject ()

Come from http://soft.zdnet.com.cn/software_zone/2009/0204/1332403.shtml

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.