A powerful weapon in Java-Object serialization

Source: Internet
Author: User

Object serialization converts the state of an object to throttling. You can use these values to generate objects in the same state. This process can also be achieved through the network. You can first create an object on a Windows machine, serialize it, and then send it to a Unix machine through the network, then re-assemble it exactly ". Is it amazing.
You may say that you only know a little bit, but you have never been in touch with it. In fact, this is not necessarily the case. RMI, Socket, JMS, and EJB should have been used. Why can each other transmit Java objects? Of course, they are all contributed by the object serialization mechanism.
The first time I used Java object serialization, I made a XXX project (in fact, I only used this time, so do not throw any vegetables or fruits ^ _*), at that time, it was required to save several very complex trees (jtrees) and corresponding data (which is our frequently used storage function) so that the last operation can be continued during the next running of the program. At that time, XML technology was very popular on the Internet, and its functions were also powerful. The structure of the tree was originally similar to the format of XML data storage. Of course I would like to try it out as a new technology. However, after careful analysis, we found that if XML is used to store data, the result is really hard to imagine: Which node of the tree is expanded to the nth level, and what is the current attribute of the node. I really don't know whether to use A, B, C or 1, 2, or 3. Fortunately, the Java object serialization mechanism was discovered, and the problem was solved. Simply serialize the root node of each tree and save it to the hard disk, next time, the root node after deserialization can easily construct the same tree as the original one.
In fact, the storage of data, especially complex data, is a typical application of Object serialization. Recently, the YYY project encountered the need to access very complex data. By using object serialization, the problem is also difficult to simplify.
Another easy-to-be-ignored feature of Object serialization is object replication (Clone). in Java, the Clone mechanism can be used to copy most objects. But as we all know, Clone has deep Clone and shallow Clone, if your object is very complex, assume that there is a Collection (exaggerated) on the 100 layer. If you want to implement deep Clone, you can't imagine it. If you want to use serialization, no more than 10 lines of code can be solved. There is also the Swing component. If you have two very (or exactly the same) Difficult-to-construct Swing components, what should you do? Maybe you think of Clone, however, the Swing component does not provide the Clone method. Don't worry, use serialization, six lines of code,

  1. ByteArrayOutputStream byteOut =NewByteArrayOutputStream ();
  2. ObjectOutputStream out =NewObjectOutputStream (byteOut );
  3. Out. writeObject (combo );
  4. ByteArrayInputStream byteIn =NewByteArrayInputStream (byteOut. toByteArray ());
  5. ObjectInputStream in =NewObjectInputStream (byteIn );
  6. JComboBox comb2 = (JComboBox) in. readObject ();

If you are not addicted to it, you can do the same with two lines of code. Is there a line of code? It doesn't seem to work!
Although Java serialization is very simple and powerful, there are many points to note to make good use of it. For example, if an object has been serialized, but for some reason, this class has been modified a little and then re-compiled, then deserialization of the object just now will cause an exception. You can solve this problem by adding the serialVersionUID attribute. If your class is a Singleton class, whether to allow users to copy the class through the serialization mechanism, if not, you need to be cautious about its implementation.


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.