Why does the serian implement the Serializable interface ?, Implement the serializable Interface

Source: Internet
Author: User
Tags object serialization

Why does the serian implement the Serializable interface ?, Implement the serializable Interface
Reference: http://jzinfo.javaeye.com/blog/519470

Java's "Object serialization" allows you to convert an object that implements the Serializable interface into a group of bytes, so that you can recover these bytes when using this object in the future, and re-build the object accordingly. This is true even in a cross-network environment, which means that the serialization mechanism can automatically compensate for the differences in the operating system. In other words, you can create an object on a Windows machine, serialize it, upload it to a Unix machine over the network, and then recreate it there. You don't have to worry about how data is represented on different platforms, how bytes are ordered, or what other details.

Object serialization itself is very interesting because it allows you to implement "lightweight persistence )". Persistence means that the object's life cycle is not determined by whether the program runs or not; the object remains alive between two calls of the program. You can achieve the persistence effect by writing serialized objects to the disk and then reading them when the program runs again. The reason for "lightweight" is that you cannot use keywords like "persistent" to directly define an object and then let the system process all the details (although this may happen in the future ). Instead, you must explicitly serialize and deserialize ). If you need more formal persistence functionality, consider tools like Java Data Object (JDO) Or Hibernate (http://hibernate.sourceforge.net ).

Object serialization must be added to the language to implement two important functions. Java Remote Method Invocation (RMI) allows you to call objects on other machines as you call objects on your machine. When you send a message to a remote object, you need to transmit parameters and return values through object serialization. RMI will be discussed in Thinking in Enterprise Java.

We will talk about JavaBean in Chapter 14th. For JavaBean, Object serialization is also essential. Bean status information is usually configured during design. These state information must be saved for use when the program starts; Object serialization is responsible for this work.

Serialization of an object is still relatively simple, as long as it implements the Serializable interface (this is a "tagging interface", there is no way ). However, when the serialization concept is introduced, the classes of many of its standard class libraries, including the wrapper class of primitive, all the container classes, and many other classes, will change accordingly. Even Class objects are serialized.

To serialize an object, you must first create an OutputStream and embed it into ObjectOutputStream. In this case, you can use the writeObject () method to write the object into OutputStream. During reading, you must embed InputStream into ObjectInputStream and then call the readObject () method. However, this is only the reference of an Object. Therefore, you have to upload the Object first before using it.

The smartest thing about Object serialization is that it not only saves copies of objects, but also stores the referenced objects along with the reference in the object, then, continue to trace the reference of those objects, and so on. This situation is often referred to as "object network connected by a single object '". This mechanism covers not only the member data of the object, but also the reference in the array. If you want to implement Object serialization by yourself, writing programs that track these links will be a very painful task. However, Java's object serialization can achieve this accurately. Without a doubt, its traversal algorithm has been optimized.

---------------------------------------------------------------------

Classes that implement the java. io. Serializable interface can be serialized. Classes that do not implement this interface Cannot serialize or deserialize any of their statuses.

All subclasses of the serialization class are serializable. This serialization interface does not have any methods and fields and is only used to identify the meaning of serialization. Allows the serialization of non-serialized classes. The subtype can be assumed to be responsible for saving and restoring the state of the public, protected, and (if accessible) packages of the parent type. As long as the class (Extension) has a non-argument constructor that can initialize its status, the subtype can assume the preceding responsibilities. In this case, declaring a serializable class is an error. This error will be detected at runtime. You can save the object to the byte stream and restore it!

For example:Integer implements Serializable, so you can write an Integer object to a file using IO, and then read it from the file, such as the intValue () of the object when you start writing () if it is 5, it is also 5 after reading. This indicates the role of the sequential class, that is, the object used to transmit the class.

When a JavaBean is customized in the constructor and connected to other beans, all its statuses can be saved, the next time it is loaded into the constructor or during running, it should be the last modified information. To achieve this, you need to save the information of some Bean fields and implement the Java. io. Serializable interface when defining the Bean. For example:

Public class Button implements Java. io. Serializable {......}

The field information in the Bean that implements the serialization interface will be automatically saved. If you do not want to save some words (here, the field information in Bean will be automatically saved, what does it mean? How is automatic saving implemented ?)

Segment information can be prefixed with the transient or static keyword before these fields. The information of the transient and static variables cannot be saved. Generally, all attributes exposed by a Bean should be saved, and the internal status can be selectively saved. When modifying software, Bean developers can add fields, remove references to other classes, and change the private, protected, or public status of a field. These do not affect the storage structure of the class. However, when you delete a field from the class, change the position of a variable in the class system, change a field to transient/static, or the original field is transient/static, and change it to another feature, will cause changes to the storage relationship.

The so-called Serializable is the interface for saving and reading general data provided by java. As for where to read and save data, it is hidden behind the function parameters. This way,Any type can be saved to a file as long as the Serializable interface is implemented, or data streams can be sent to other places through the network. You can also use pipelines to transmit data to other programs in the system. This greatly simplifies the class design.You only need to design a READ function to solve all the problems mentioned above.

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.