Serialization and deserialization of Java objects

Source: Internet
Author: User

One, serialization of objects, deserialization
1. The serialization of an object is the conversion of an object into a byte sequence, which in turn is called deserialization
2. Serialization requires a stream class, a serialized stream (ObjectOutputStream), is a (byte) filter stream, has a writeobject method, writes an object
Deserialization Stream (ObjectInputStream)----readobject
3. Serialization interface (Serializable)
Object must implement a serialization interface for serialization, or an exception will occur
This interface, without any method, is just a standard
/**
* If you want this object to be transmitted over the network in the future, you need to serialize the object as well. (transmitted in bytes in the network)
* So we need to serialize this object when we want to save this object, or transfer it over the network
*/
Two, transient key word
Some properties in an object do not need to be serialized and can be decorated with transient key sub-modifiers.
An element decorated with transient that does not perform the default serialization of the JVM. You can also complete the serialization of this element yourself
(In a network transmission, some elements do not need to be transmitted, it is not serialized, which can save network traffic)
How do you do serialization yourself?
There are two method signatures that can be implemented. There are two ways to refer to ArrayList:
WriteObject (Java.io.ObjectOutputStream s), ReadObject (Java.io.ObjectInputStream s)

ArrayList serialization optimization problem (ArrayList source)
ArrayList the bottom is an array, but when we add data to the list, there is a case for adding dissatisfaction. Elements that are empty in the array are not serialized, which can improve performance

 Packagecom.amazing.jdk.learn2Serializable.basicSerializable_08_15;Importjava.io.Serializable;/*** Created by yaming on 17-8-15.*/ Public classStudentImplementsSerializable {PrivateLong ID; PrivateString name; Private Chargender; Private transient DoubleMoney ;  PublicLong getId () {returnID; }     Public voidsetId (Long id) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public CharGetgender () {returngender; }     Public voidSetgender (Chargender) {         This. Gender =gender; }     Public DoubleGetmoney () {returnMoney ; }     Public voidSetmoney (DoubleMoney ) {         This. Money =Money ; } @Override PublicString toString () {return"student{" + "id=" + ID + ", name=" + name + ' \ ' + ", gender=" + Gender + ", money=" + Money + '} '; }    //self-realization of serialization    Private voidWriteObject (java.io.ObjectOutputStream s)throwsjava.io.ioexception{s.defaultwriteobject ();//serialization of elements that the JVM can default to serializeS.writedouble (Money);//complete the serialization of money yourself, or you can use S.writeobject (money)    }    //self-implement deserialization    Private voidReadObject (java.io.ObjectInputStream s)throwsjava.io.IOException, ClassNotFoundException {s.defaultreadobject ();//Deserializes an element that the JVM can default to deserialize         This. money=s.readdouble ();//complete the deserialization of money yourself    }}




Serialization and deserialization of Java objects

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.