Java Serialization Technology

Source: Internet
Author: User

In real-world development, we will write an object to a file for persistence. Then this class must implement a serializable interface to write! So how does Java persist the object?

Java serialization is the conversion of an object into a string of binary representations of byte arrays, which are persisted by saving or transferring the byte data. To persist, the object must inherit the Java.io.Serializable interface. Deserialization is the opposite process of re-structuring this byte data into an object. We know that when deserializing, we must have the original class as a template in order to restore this object, from this process we can guess that the serialized data does not save the complete structure information of the class as the class file does.

So what information does the serialized data contain, as shown in the following code:

import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectOutputStream;import java.io.Serializable;public class Student implements Serializable {    private static final long serialVersionUID = 1396269444885402064L;    public int num = 3190;     public static void main(String[] args) {        try {            FileOutputStream fos = new FileOutputStream("student.txt");            ObjectOutputStream oos = new ObjectOutputStream(fos);            Student s = new Student();            oos.writeObject(s);            oos.flush();            oos.close();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }}

The serialized file binary byte data is as follows:

Analytical:

ac ed: STREAM_MAGIC 声明使用了序列化协议。 00 05: STREAM_VERSION 序列化协议版本。 73: TC_OBJECT 声明这是一个新的对象。 72: TC_CLASSDESC声明这里开始一个新class。 72 00 07 53 74 75 64 65 6E 74: Student的完整类名。   13 60 8B A9 91 20 51 D0: serialVersionUID,序列化ID,如果没有 指定,则会由算法随机生成一个8字节的ID。 02: 标记号,该值声明该对象支持序列化。 00 01: 该类所包含的域的个数为1。 49:域属性,49代表“I”,也就是Int类型。 00 03:域名字的长度,为3。 6e 75 6d: num属性的名称。 78:TC_ENDBLOCKDATA,对象块结束的标志。 70:TC_NULL,说明没有其他超类的标志. 00 00 05 6e:3190的数值。

Although the serialization of Java can guarantee the persistence of object state, it is difficult to deal with some complex object structures, the following is a summary of some complex object situations:

1. When the parent class inherits the serializable interface, all subclasses can be serialized.

2. Subclasses implement the Serializable interface, the parent class does not, the parent class's properties cannot be serialized (without error, the data is lost), but the attributes in the subclass are still correctly serialized.

3. If the serialized attribute is an object, the object must also implement the Serializable interface, otherwise it will error.

4. When deserializing, if the properties of an object are modified or deleted, the modified part of the property is lost but does not cause an error.

Java Serialization Technology

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.