Java serialization -- parse the java. io. Serializable Interface

Source: Internet
Author: User

Since java is used, serialization is everywhere. As to why serialization and serialization are used to solve problems, as a common coders, it is generally not very in-depth research, recently, mina and the company are looking at some of the nio Framework's source code, which involves hession and java serialization. As for why hession serialization was born and is widely used in apache projects, and what are the defects in java serialization? To answer the questions raised above and learn more about java serialization mechanism, a small header is provided here, starting with Serializable, the serialization interface of java.

The comments of the Serializable interface in the jdk package mainly explain the following:

1. The class enables serialization by implementing the Serializable interface. Otherwise, the status of the class cannot be serialized, and it cannot be used for deserialization.

2. If the inherited parent class does not implement the Serializable interface, but the subclass can be serialized, there are three considerations:

A) Implement the Serializable interface in the subclass.

B ). the subclass must have an accessible non-parametric constructor to save and restore the state of the public, protected, or package field of the parent class, otherwise, a RuntimeException exception is thrown during serialization or deserialization.

C). During deserialization of serialized subclasses, theoretically, the state or value of private (inaccessible) object variables in the parent class cannot be initialized.

3. When serializing attributes in a serializable class, if an object variable that is not serializable is encountered, A NotSerializableException exception will be thrown for the class that is not serializable.

4. for non-array classes that can be serialized, it is strongly recommended that the declared static, long, and final serialVersionUID fields be displayed to identify the version number of the current serialization class, otherwise, the InvalidClassException exception may occur during serialization and deserialization across operating systems and compilers.

5. for static and transient object variables in a serializable class, the status or value cannot be saved during serialization, the value obtained by static object variables in deserialization is the value of the static variable corresponding to the corresponding class in the current jvm, and transient (transient) keyword is generally used to identify state variables that do not need to be passed during serialization.

Simple test code:

01
Import java. io. FileInputStream;
02
Import java. io. FileNotFoundException;
03
Import java. io. FileOutputStream;
04
Import java. io. IOException;
05
Import java. io. ObjectInputStream;
06
Import java. io. ObjectOutputStream;
07
Import java. io. Serializable;
08
 
09
/**
10
* Serialization Test
11
*
12
* @ Author sume
13
*
14
*/
15
Public class SerializableImpl implements Serializable {
16
 
17
Private static final long serialVersionUID =-6433786313435044319L;
18
 
19
Static String staticVal = "static1 ";
20
Transient String transientVal = "transient1 ";
21
String val = "val1 ";
22
 
23
/**
24
* Main
25
*/
26
Public static void main (String [] args) throws FileNotFoundException, IOException, ClassNotFoundException {
27
// Serialization
28
SerializableImpl sila1 = new SerializableImpl ();
29
ObjectOutputStream objectOutputStream = new ObjectOutputStream (new FileOutputStream ("Serializable.txt "));
30
ObjectOutputStream. writeObject (sila1 );
31
ObjectOutputStream. close ();
32
 
33
// Deserialization
34
SerializableImpl. staticVal = "static2 ";
35
ObjectInputStream objectInputStream = new ObjectInputStream (new FileInputStream ("Serializable.txt "));
36
SerializableImpl sila2 = (SerializableImpl) objectInputStream. readObject ();
37
ObjectInputStream. close ();
38
 
39
// Compare the values of each attribute
40
System. out. println (sila2.staticVal );
41
System. out. println (sila2.transientVal );
42
System. out. println (sila2.val );
43
}
44
}
Output result:
1
Static2
2
Null
3
Val1
The output result shows that:

1. The value of the static variable staticVal in the Post-deserialization class is the value of the static variable corresponding to the current jvm, Which is static2, rather than the value of static1 during serialization.

2. The status of the variable identified by the transient keyword is not saved in serialization. Therefore, after deserialization

The value of transientVal is null.

3. The third is the transmission of Common Object States during serialization and deserialization.

Briefly confirms the content mentioned above

In addition, for operations that require special processing of classes during serialization and deserialization, or that require an alternative object to be specified,

This article is not involved and will be discussed later

Author: "sumekey's blog"

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.