Cold knowledge of serialization and deserialization in Java

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/zhaokaiqiang1992

As to what is serialization, and why the knowledge of serialization is no longer elaborated, this article mainly discusses some special points of the situation.

How to achieve serialization and deserialization in 1.java

The following code is a simple instance of serialization

public static void Main (string[] args) {System.out.println ("-----------------serialization----------------------↓"); Student student1 = new Student ("Zhao"); Student Student2 = new Student ("Kai"); Student Student3 = new Student ("Qiang"); ObjectOutputStream Objectwriter = null;try {objectwriter = new objectoutputstr EAM (New FileOutputStream ("./serializable")); Objectwriter.writeobject (student1); o Bjectwriter.writeobject (Student2); Objectwriter.writeobject (STUDENT3);} catch (Exception e) {e.printstacktrace ();} finally {try {objectwriter.close ();} catch (IOException e) {E.printstacktrace ();}} System.out.println ("-----------------deserialization----------------------↓"); 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 when deserializing?

My test serialization class is as follows, in the parameterless and parameterless constructors, the statements are printed, and then we test using the above serialization and deserialization code.

public class Student implements Serializable {private int age;private String name;public Student () {System.out.println ("S Tudent () ");} 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 + "]";}}

Here are the test results

-----------------serialization----------------------↓student (int age, string name) Student (int age, string name) Student (int age, String name)-----------------deserialization----------------------↓student [age=10, Name=zhao]student [age=15, Name=kai] Student [Age=20, Name=qiang]

Therefore, we can assume that at the time of deserialization, there is no need to invoke the constructor of this class.


2. When deserializing, will the constructor of the parent class be called?

We create a new person class and then use student to inherit the code from Person,student 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 code for the Person class 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 + "]";}}

again, I output the constructor of person and then use the code above to test it, and here are the test results

-----------------serialization----------------------↓person () Student (int age, string name) person () Student (int age, string name The person () Student (int, String name)-----------------deserialized----------------------↓person () person () person () Student [Age=10, Name=zhao] Student [Age=15, Name=kai]student [Age=20, Name=qiang]

as we can see, although the student constructor is not called, the parameterless constructor of the person is called, which means that the constructor of this class is not called when deserializing, but the parameterless constructor of its parent class is called. In the absence of inheritance, the parent class of all classes, the constructor of object, is called.


3. Can I serialize and deserialize a protect property in a parent class when the parent class of a class that needs serialization is not implementing serialization?

In order to test, we need to modify the code of the Person class, the following modified code

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 student's ToString (), following 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 are serializing and deserializing, and here are the test results

-----------------Serialization----------------------↓person (Boolean Sex) person (Boolean Sex) person () Student (int age, String Name)-----------------deserialization----------------------↓person () person () person () Student [age=10, Name=zhao, Sex=false] Student [Age=15, Name=kai, sex=false]student [Age=20, Name=qiang, Sex=false]

As you can see from the results, although the parent class is not serialized, the sex attribute also participates in serialization and deserialization operations, so it does not affect.


From the results of the above several tests, we can conclude that serialization and deserialization must call the parameterless constructor of its parent class.







Cold knowledge of serialization and deserialization in Java

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.