10--serialization of Java Review notes

Source: Internet
Author: User
Tags serialization

Java Serialization

The Java serialization technique allows you to write the state of an object into a byte stream (serialization), and you can read (deserialize) the data in that byte stream from somewhere else.

Serialization Purpose
    • When you want to save the state of an object in memory to a file or to a database

    • When you want to spread the object over the network,

Serialversionuid

Notice that there is a serialversionuid in the program above, and after implementing the Serializable interface, eclipse will prompt you to add a serialversionuid, although the above program can still run normally if not added.

The serialization ID provides two build strategies under Eclipse

    • One is a fixed 1L

    • One is to randomly generate a long type of data that is not duplicated (actually using the JDK tool, based on the class name, interface name, member method and attributes, etc.)

In the above program, the output object and the read-in object use the same person class.

If it is transmitted over the network, if the serialversionuid of the person class is inconsistent, then deserialization does not work. For example, in client A, the person class's serialversionuid=1l, and the person class's serialversionuid=2l in client B cannot refactor the person object.

serialization of static objects

Serialization can only hold non-static member arguments of an object, cannot save any member methods and static member variables, and serialization saves only the value of the variable, and no modifier for the variable can be saved.

If the name in the person class is defined as a static type, attempting to refactor can not get the original value, only the null is obtained. Description The value of the static member variable is not saved. This is actually easier to understand, the serialization holds the state of the object, the static variable belongs to the state of the class, so serialization does not save the static variable.

transient

The transient keyword is often seen in classes that implement the Serializable interface. This keyword is not common.

The purpose of the Transient keyword is to block the persistence of variables in the instance that are declared with this keyword, and when the object is deserialized (a sequence of bytes is read from the source file), such instance variable values are not persisted and restored.

When some variables do not want to be serialized, it is not appropriate to use the Static keyword declaration (the same as static), then you need to declare the variable with the transient keyword.

After being deserialized, the value of the transient variable is set to the initial value, such as the int type is 0, and the object type is null.

Note : For certain types of properties, their state is instantaneous, and such a property cannot save its state. For example, a thread attribute or a property that requires access to IO, local resources, network resources, and so on, we must use the Transient keyword for these fields, otherwise the compiler will report.

Inheritance Issues
    • When a parent class is serialized, the subclass is automatically serialized and does not require an explicit implementation of the serializable interface.

    • A subclass implements the Serializable interface, and its parent class does not implement the Serializable interface, and to serialize the parent class object, it is necessary to have the parent class implement the Serializable interface as well.

In the second case, if the parent class does not implement the Serializable interface, a default parameterless constructor is required. This is because the construction of a Java object must have a parent object before it can be a child object, and deserialization is no exception. In order to construct the parent object, you can only call the parent class's parameterless constructor as the default parent object when deserializing. So when we take the variable value of the parent object, its value is the value after calling the parent class without a parameter constructor. In this case, the parent class does not have a parameter at the time of serialization as needed

10--serialization of Java Review notes

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.