The public interface serializable class enables its serialization function by implementing the java. Io. serializable interface. Classes that do not implement this interface cannot be serialized or deserialized in any State. All sub-types of serializable classes are themselves serializable. The serialization interface has no methods or fields and is only used to identify the serializable semantics.
To allow serialization of subtypes of non-serializable classes, assume that this subtype is responsible for saving and restoring the public, protected, and (if accessible) super classes) the status of the package field. Only when the child type extension class has an accessible non-parameter constructor to initialize the state of the class can it be assumed that the child type has this responsibility. If this is not the case, it is wrong to declare a class as a serializable class. This error will be detected at runtime.
During deserialization, fields of the non-serializable class will be initialized using the public or protected non-parameter constructor of this class. The serializable subclass must be able to access the construction method without parameters. Fields of serializable subclass will be restored from the stream.
When traversing a graph, you may encounter objects that do not support serializable interfaces. In this case, notserializableexception will be thrown and classes that identify non-serializable objects will be thrown.
Classes that require special processing during serialization and deserialization must use the following accurate signatures to implement special methods:
Private void writeobject (Java. Io. objectoutputstream out)
Throws ioexception
Private void readobject (Java. Io. objectinputstream in)
Throws ioexception, classnotfoundexception;
The writeobject method is used to write the state of the object of a specific class so that the corresponding readobject method can restore it. You can call out. defaultwriteobject to call the default mechanism for saving the fields of the object. This method does not need to involve the status of its superclass or subclasses. The status is saved by writing fields into objectoutputstream using the writeobject method or the method supported by dataoutput for basic data types.
The readobject method reads and restores class fields from the stream. It can call in. defaultreadobject to call the default mechanism to restore non-static and non-transient fields of the object. The defaultreadobject method uses the information in the stream to allocate the fields of the objects saved by the corresponding named fields in the current object in the stream. This is used when new fields need to be added after the class is developed. This method does not need to involve the status of its superclass or subclasses. The status is saved by writing fields into objectoutputstream using the writeobject method or the method supported by dataoutput for basic data types.
When writing an object to a stream, you must specify the serializable class of the alternative object to be used. Use an accurate signature to implement this special method:
Any-access-modifier object writereplace () throws objectstreamexception;
This writereplace method will be called by serialization, provided that this method exists and can be accessed through a method defined in the class of the serialized object. Therefore, this method can have private, protected, and package-Private access. The subclass follows the Java access rules for this method.
When reading an instance of a class from a stream, you must specify the correct signature for the substitute Class to implement this special method.
Any-access-modifier object readresolve () throws objectstreamexception;
This readresolve method follows the same calling rules and access rules as writereplace.
During serialization, a version number called serialversionuid is used to associate with each serializable class, in the deserialization process, this serial number is used to verify whether the sender and receiver of the serialized object have loaded classes compatible with serialization for this object. If the serialversionuid of the class loaded by the receiver is different from the version of the class of the corresponding sender, deserialization will lead to invalidclassexception. The serializable class can explicitly declare its own serialversionuid by declaring a field named "serialversionuid" (which must be a static and final long field:
Any-access-modifier static final long serialversionuid = 42l;
If the serialversionuid is not explicitly declared for the serializable class, the default serialversionuid value of the class is calculated based on all aspects of the class during the serialization runtime, as described in "Java (TM) Object serialization specification. However, it is strongly recommended that all serializable classes explicitly declare the serialversionuid value. The reason for calculation is that the default serialversionuid is highly sensitive to the class details, which may vary according to compiler implementations, in this way, unexpected invalidclassexception may occur during deserialization. Therefore, to ensure the consistency of serialversionuid values across different Java compilers, the serialization class must declare a clear serialversionuid value. We strongly recommend that you use the private modifier to display the Declaration serialversionuid (if possible), because this declaration is only applicable to the immediate declaration class -- serialversionuid field as an inherited member.
--------------------------
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.
What does serialize mean?
This word is translated as serialized or serialized in the Chinese text. These two translations are almost impossible to express their actual meaning. If you do not understand it, you still do not understand it.
In fact, this is a concept in IO Storage. All the content in the computer is 0 or 1, and I think it's possible that even the five-year-old urchin knows it now. These numbers are arranged in order to represent everything in the computer. Of course, Java objects are no exception. In the memory, no matter how it is expressed, it does not affect our operations, so we don't have to worry about it. However, when we need to store objects to hard disks, databases, or other related media, we need to consider this representation format or conversion method.
How can Java objects be converted to the series of 0 and 1? It depends on serialization. But you don't have to worry about it. Java is very considerate in this regard. You don't need to think about the conversion algorithm yourself. You only need to use Java. Io. objectoutputstream class to complete the complex conversion work. However, the objectoutputstream class only supports objects that implement the serializable interface. If the serializable interface is not implemented, the notserializableexception will be thrown.
Transient
However, in a class that you want to serialize, it is often possible that some temporary member variables or attributes that you do not want them to be stored, these member variables or attributes, you need to use the transient keyword. They are ignored during serialization. When read from the storage medium and restored to an object, they are restored to the default value of the attribute (basic type: 0 or false, the object is null ).
This is an overview of serialize in Java. For details, refer to the IO chapter in Sun Java tutorial.
Why must all java beans in the project implement the serializable interface?
This should begin with the session sharing mechanism of WebSphere Application Server (WAS. In a Japanese environment, the was server backs up each other and distributes client requests through Server Load balancer dispatcher. Session is an extremely important storage unit in Web development. User-related content is often stored in the session container on the server. If there is only one was server, the session container can only be established in the memory, but when there are two was servers that are mutually backed up, they must share sessions to make the client feel the server switching process. However, session sharing is obviously not implemented through memory, and external storage devices must be used. In was, a common method is to store sessions in the database. In this case, the session needs to be serialized, and it is bound to start to serialize the content stored in all sessions. To make this operation run normally, all Java Beans that may be stored in the session must support serialization and must implement the serializable interface.
It can be seen that not all Java Beans need to implement the serializable interface, but it is only necessary to put the java bean in the session. However, the experienced technical experts in the project did not clearly analyze the root cause of the problem, so they had to come up with a suitable solution based on experience.