A powerful weapon in Java--Serialization of objects

Source: Internet
Author: User
Tags object 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.

rmi , socket , JMS , EJB you always have to use one, why can each other pass java

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 you can continue the last operation the next time you run the program.

ThenXMLtechnology on the internet is very hot, but also powerful, coupled with the structure of the tree andXMLthe format of the stored data is very similar. 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 you useXMLSaving the data, the consequences are unimaginable: which node of the tree is expanded, to the first levels, what is the current attribute of the node. I don't know how to use it .A,B,Cor with the1,2,3to express.

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),Javathrough theCloneThe mechanism can replicate most objects, but it is well known thatClonehave deepCloneand shallow layersClone,if your object is very, very complex, suppose you have a -layer ofCollection(exaggerated point), if you want to achieve deepClone, I can't imagine that if you use serialization, it won't exceedTenThe line code can be resolved.

swing components, If you have two more difficult-to-construct component, what do you do, maybe you think of clone java swing component does not provide clone method. Don't worry, using serialization, 6 line code 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

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.