Cold knowledge of serialization and deserialization in java, java serialization knowledge

Source: Internet
Author: User

Cold knowledge of serialization and deserialization in java, java serialization knowledge

Reprinted please indicate the source: http://blog.csdn.net/zhaokaiqiang1992

I will not elaborate on what serialization is and why serialization is required. This article mainly discusses some special situations.

1. How to Implement serialization and deserialization in java

The following code is a simple instance for serialization.

Public static void main (String [] args) {System. out. println ("----------------- serialization -------------------- success"); Student student1 = new Student (10, "zhao"); Student student2 = new Student (15, "kai "); student student3 = new Student (20, "qiang"); ObjectOutputStream objectWriter = null; try {objectWriter = new ObjectOutputStream (new FileOutputStream (new File (". /Serializable "); objectWriter. writeObject (student1); objectWriter. writeObject (student2); objectWriter. writeObject (student3);} catch (Exception e) {e. printStackTrace ();} finally {try {objectWriter. close ();} catch (IOException e) {e. printStackTrace () ;}} System. out. println ("----------------- deserialization -------------------- bytes"); ObjectInputStream objectInputStream = null; try {objectInputStream = new ObjectInputStream (new FileInputStream (new File (". /Serializable "); Student s1 = (Student) objectInputStream. readObject (); Student s2 = (Student) objectInputStream. readObject (); Student s3 = (Student) objectInputStream. readObject (); System. out. println (s1.toString (); System. out. println (s2.toString (); System. out. println (s3.toString ();} catch (Exception e) {e. printStackTrace ();} finally {try {objectInputStream. close ();} catch (IOException e) {e. printStackTrace ();}}}

2. Do I need to call the constructor of this class during deserialization?

The serialization class I tested is as follows. The statements are printed in constructors without parameters and parameters. Then, we use the serialization and deserialization code above for testing.

public class Student implements Serializable {private int age;private String name;public Student() {System.out.println("Student()");}public Student(int age, String name) {this.age = age;this.name = name;System.out.println("Student(int age, String name)");}@Overridepublic String toString() {return "Student [age=" + age + ", name=" + name + "]";}}

The test results are as follows:

----------------- Serialize Student (int age, String name) ----------------- deserialization ---------------------- serialize Student [age = 10, name = zhao] Student [age = 15, name = kai] Student [age = 20, name = qiang]

Therefore, we can think that the constructor of this class does not need to be called during deserialization.


2. Will the constructor of the parent class be called during deserialization?

We create a new Person class and use Student to inherit from Person. The Code of Student is as follows:

public class Student extends Person implements Serializable {private int age;private String name;public Student() {System.out.println("Student()");}public Student(int age, String name) {this.age = age;this.name = name;System.out.println("Student(int age, String name)");}@Overridepublic String toString() {return "Student [age=" + age + ", name=" + name + "]";}}

The Person class code is as follows:

public class Person {private boolean sex;public Person() {System.out.println("Person()");}public Person(boolean sex) {this.sex = sex;System.out.println("Person(boolean sex)");}@Overridepublic String toString() {return "Person [sex=" + sex + "]";}}

Similarly, I output all the constructors of Person and then use the above Code for testing. The following is the test result.

----------------- Serialize -------------------- serialize Person () Student (int age, String name) ----------------- deserialization -------------------- serialize Person () Student [age = 10, name = zhao] Student [age = 15, name = kai] Student [age = 20, name = qiang]

We can see that although the Student constructor is not called, the Person constructor is called, which means that the constructor of this class will not be called during deserialization, however, it calls the non-argument constructor of its parent class. Without inheritance, the parent class of all classes is called, that is, the Object constructor.


3. When the parent class of the class to be serialized does not implement serialization, can we serialize and deserialize the protect attribute of the parent class?

For testing, we need to modify the Person class code. The modified code below

public class Person {protected boolean sex;public Person() {System.out.println("Person()");}public Person(boolean sex) {this.sex = sex;System.out.println("Person(boolean sex)");}@Overridepublic String toString() {return "Person [sex=" + sex + "]";}}

At the same time, we also need to modify the toString () of Student. below is the modified Student code

public class Student extends Person implements Serializable {private int age;private String name;public Student() {System.out.println("Student()");}public Student(int age, String name) {this.age = age;this.name = name;System.out.println("Student(int age, String name)");}public Student(int age, String name, boolean sex) {super(sex);this.age = age;this.name = name;}@Overridepublic String toString() {return "Student [age=" + age + ", name=" + name + ", sex=" + sex + "]";}}

We perform serialization and deserialization. The following is the test result.

----------------- Serialize -------------------- serialize Person (boolean sex) Person () Student (int age, String name) ----------------- deserialization ---------------------- serialize Person () student [age = 10, name = zhao, sex = false] Student [age = 15, name = kai, sex = false] Student [age = 20, name = qiang, sex = false]

As can be seen from the results, although the parent class is not serialized, the sex attribute is also involved in serialization and deserialization operations, so there is no impact.


From the above test results, we can draw a conclusion: for serialization and deserialization, the constructors without parameters of the parent class must be called.







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.