XML serialization of Java Objects (GO)

Source: Internet
Author: User
Tags object serialization

Transferred from: http://westlifesz.javaeye.com/blog/48618

Java.io.Serializable the problem-what is serialization? Under what circumstances will the class be serialized?

Serialization is a mechanism for dealing with the flow of objects, so-called object flow is the flow of the object's contents. It is possible to read and write to a Fluidized object, or to transfer the streamed object between the networks. Serialization is a problem that is raised when reading and writing to an object stream. Serialization implementation: Implement the Serializable interface for the class that needs to be serialized, there is no method to implement it, implements serializable just to annotate that the object is serializable, and then use an output stream ( such as: FileOutputStream) to construct a ObjectOutputStream object, followed by the WriteObject (object obj) of the ObjectOutputStream object method to write out (that is, save its state) the object with the parameter obj, and the input stream to restore.

Serialization: Serialization is the process of converting an object into a format that is easy to transfer. For example, you can serialize an object and then use HTTP to transfer the object between the client and the server over the Internet. At the other end, deserialization reconstructs the object from the stream.
is a mechanism for object persistence.

Specifically, the object should be serialized, the general program at run time, the object is generated, these objects disappear as the program stops running, but if we want to put some objects (because it is the object, so there are different characteristics) to save, after the program terminates, these objects still exist, You can read the values of these objects while the program is running again, or take advantage of these saved objects in other programs. In this case, the serialization of the object is used.

Only serialized objects can be stored on the storage device. An interface that needs to be inherited for the purpose of serializing an object is just a symbolic interface, which means that the object can be serialized and has no other purpose. Object serialization is required because sometimes the object needs to be transmitted over the network, it needs this serialization processing, from the server hard disk to take the serialized object out, and then through the network to the client, and then by the client to the serialized object read into memory, to perform the corresponding processing.

Object serialization is a feature of Java that allows objects to be written into a set of bytecode that, when read in other locations, creates a new object, and the state of the new object is exactly the same as the original object. In order to implement object serialization, it is necessary to have access to the private variables of the class, so that the object state can be saved and restored correctly. Accordingly, the object serialization API is able to restore these values to a private data member when the object is rebuilt. This is a challenge to access permissions for the Java language. Usually used in the server client's object exchange above, the other is in the native storage.

The most important use of object serialization is to guarantee the integrity and transitivity of objects when passing, and saving objects (object). For example, when transmitting over a network, or storing an object as a file, the serialization interface is implemented.
*
Quote:


Compare java.io.Externalizable and Java.io.Serializable
[Url]http://www.zdnet.com.cn/developer/code/story/0,3800066897,39304080,00.htm[/url]

Even if you have not used object serialization (serialization), you may also know it. But did you know that Java also supports another form of object persistence, externalization?

Here's how serialization and externalities are associated at the code level:

Public interface Serializable {}

Public interface Externalizable extends Serializable {
void Readexternal (objectinput in);
void Writeexternal (ObjectOutput out);
}

The main differences between serialization and externalities

Externalities and serialization are two different ways to achieve the same goal. Let's examine the main differences between serialization and externalities.

Support for object serialization through the serializable interface is built into the core API, but all java.io.Externalizable implementations must provide read and write-out implementations. Java already has built-in support for serialization, meaning that if you make your own class Java.io.serializable,java you will try to store and reorganize your objects. If you use externalities, you can choose to do the work of reading and writing entirely by yourself, and the only support Java provides to the externality is the interface:

Voidreadexternal (ObjectInput in)
void Writeexternal (ObjectOutput out)

Now how to implement Readexternal () and writeexternal () is entirely up to you.

Serialization automatically stores the necessary information to deserialize the stored instance, and the externality saves only the identity of the stored class. When you serialize an object through the Java.io.Serializable interface, information about the class, such as its properties and the types of those properties, is stored together with the instance data. Java only stores very little information about each of the stored types when you choose to go externalizable this way.

Advantages and disadvantages of each interface

Serializable interface

· Advantages: Built-in support

· Advantages: Easy to implement

· Cons: Too much space occupied

· Cons: Slower speed due to extra overhead

Externalizable interface

· Pros: Less overhead (programmers decide what to store)

· Pros: Possible speed boost

· Cons: Virtual machines do not provide any help, which means that all work falls on the developer's shoulders.

How to choose between the two depends on the application's needs. Serializable is usually the simplest solution, but it can lead to unacceptable performance issues or space problems, and in the case of these problems, externalizable can be a viable path.

One thing to keep in mind is that if a class is externalizable, then the Externalizable method will be used to serialize instances of the class, even if the type provides the Serializable method:

private void WriteObject ()
private void ReadObject ()

XML serialization of Java Objects (GO)

Related Article

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.