Serialization and deserialization of objects in Java

Source: Internet
Author: User

"Serialization and deserialization of objects"
1. Definition:
Serialization-Writes an object to an output stream. Deserialization is the reading of an object from an input stream. The members of a class must be serializable, and to implement the Serializable interface, objects of such a class can be serialized and deserialized. This interface is an interface of a representation type. Serialversionuid is a universal identifier for a serialized class, and crossdress is used to ensure that a loaded class corresponds to a serializable object.

You specify the Serialversionuid, you can after serialization, to add a field, or method, without affecting the later restore, the restored object can still be used, and more methods can be used. Serialversionuid generation, can write 1, can also write 2, but it is best to follow the digest algorithm, generate a unique fingerprint number, eclipse can be generated automatically, the JDK also comes with this tool. The general wording is similar to
Private static final long serialversionuid = -763618247875550322l;

2. Serialization steps:
1) Create an object output stream: You can wrap a different type of output stream.
2) write the object through the WriteObject () of the object output stream. Note Use type conversions to convert objects to the corresponding classes.
3. Deserialization steps:
1) Create an object input stream: You can wrap a different type of output stream.
2) write the object through the ReadObject () of the object output stream.
4. What objects need to be serialized?
The basic idea of serialization is to complete the assurance of instance information. Because the instance information disappears after the run is finished. What information do you want to store? Must not be information about the method.

5. Precautions for use:
A. Some properties are not serializable (or cannot be serialized), they are modified with transit, and if you want to further control serialization and deserialization, the ReadObject () method and the WriteObject () method are provided in the class, and the custom method is called (reversed) when serializing. Note that these two methods are not defined in the Java.io.Serializable interface, but are defined in the ObjectInputStream and ObjectOutputStream classes, and these two classes implement a ObjectInput and ObjectOutput two interfaces. The following is an example of a mutable array, taken from the Java in a nutshell 2rd Edition, which implements an efficient method for adjusting the degree of variable arrays in serialization:

public class Intlist implements Serializable
{
An array to store the numbers.
Private int[] Nums = new Int[8];
Index of next unused element of nums[].
private transient int size = 0;
/** Return An element of the array */
public int elementat (int index) throws ArrayIndexOutOfBoundsException {
if (index >= size) throw new ArrayIndexOutOfBoundsException (index);
else return Nums[index];
}
/** Add an int to the array, growing the array if necessary. */
public void Add (int x) {
Grow array, if needed.
if (nums.length = = size) resize (nums.length*2);
Store the int in it.
nums[size++] = x;
}
/** an internal method to change the allocated size of the array. */
protected void Resize (int newsize) {
int[] Oldnums = nums;
Create a new array.
Nums = new Int[newsize];
Copy array elements.
System.arraycopy (oldnums, 0, nums, 0, size);
}
/** Get rid of unused array elements before serializing the array. */
private void WriteObject (ObjectOutputStream out) throws IOException {
Compact the array.
if (nums.length > Size) resize (size);
Then write it out normally.
Out.defaultwriteobject ();
}
/** Compute the transient size field after deserializing the array. */
private void ReadObject (ObjectInputStream in)
Throws IOException, ClassNotFoundException {
Read the array normally.
In.defaultreadobject ();
Restore the Transient field.
size = Nums.length;
}
}

B. Because serialization is for an instance of a class, which is an object, the members of static are class-dependent and will not be serialized.
C. Note that once a class in the API is serialized, its parent class is serialized. If you design a class that you want to serialize, the parent class must be serialized. Once the parent class is serialized, the subclasses are automatically serialized. Therefore, internal classes and extensible classes are not recommended for serialization.

Serialization and deserialization of objects in Java

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.