A powerful weapon in Java--Serialization of objects

Source: Internet
Author: User
Tags object serialization serialization

The so-called object serialization is to convert the state of an object into a byte stream, which can then be used to generate objects of the same state. The process can also be implemented over the network by creating an object on a Windows machine, serializing it, and then sending it over the network to a UNIX machine, where it is correctly re-assembled. Isn't it magical.

Perhaps you would say that only a little bit, but never touched, in fact, it may not be so. RMI, Socket, JMS, EJB you should always use a bar, why each other can pass Java objects, of course, is the credit of the object serialization mechanism.

The first time I used a Java object serialization was to do a project, which required the preservation of several very complex trees (jtree) and corresponding data (which is our usual saving function) so that we can continue the last operation the next time we run the program.

At that time, XML technology was very hot and powerful on the Internet, and the structure of the tree was similar to the format of the XML stored data. As an interested in the new technology, of course I would like to try a little bit. However, after careful analysis, it is found that if the data is stored in XML, the consequences are unimaginable: which node of the tree is expanded, to the first levels, what is the current attribute of the node. I really do not know whether to use a, B, C or 1, 2, the said.

Fortunately, the discovery of the Java object Serialization mechanism, the problem solved, simply to save the root node of each tree to the hard disk, the next time by the deserialization of the root node can be easily constructed and original identical tree.

In fact, preserving data, especially the preservation of complex data, is a typical application of object serialization. Recently, another project has encountered the need to access very complex data, by using the serialization of objects, the problem is also difficult to simplify.

The serialization of objects and another feature that is easily overlooked is Object replication (clone), where most objects can be copied through the clone mechanism, but it is well known that clone has deep clone and shallow clone, if your object is very, very complex, Assuming there is a 100-layer collection (exaggerated point), if you want to implement deep clone, you can not imagine, if the use of serialization, not more than 10 lines of code to solve.

There is the swing component, and if you have two more difficult-to-construct swing components that are much like (or exactly the same), you might think of clone, but the swing component of Java does not provide the Clone method. Don't worry, using serialization, 6 lines of code are done:

Bytearrayoutputstream

Byteout = new Bytearrayoutputstream ();

ObjectOutputStream out

= new ObjectOutputStream (byteout);

Out.writeobject (combo);

Bytearrayinputstream Bytein = new Bytearrayinputstream (Byteout.tobytearray ());

ObjectInputStream in

=new ObjectInputStream (Bytein);

JComboBox COMB2 = (JComboBox) in.readobject ();

Although the serialization of Java is very simple and powerful, there are a lot of places to be aware of. For example, once serialized an object, but for some reason, the class made a little change, and then re-compiled, then the object is deserialized just now, there will be an exception.

You can solve this problem by adding the Serialversionuid property. If your class is a single-state (Singleton) class, allows the user to copy the class through a serialization mechanism, if you do not allow you to be cautious about the implementation of the class.

A powerful weapon in Java--Serialization of 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.