Serializable interface transient keywords in Java, and object io

Source: Internet
Author: User

1. What is serialization and deserialization
Serialization is a process of converting an object into a byte stream; deserialization is the process of reverting a byte stream to an object.

2. What situations require serialization
A) When you want to save the object in memory in a file or in a database;
b) When you want to use sockets to transfer objects on the network;
c) When you want to transfer objects through RMI;

3. How to implement serialization
It is possible to implement the serializable interface for a class that requires serialization, and the Serializable interface, like the Cloneable interface, does not contain any methods, it is a labeled interface.

4. Code Analysis

Package Com.tonyluis;import java.io.*;p Ublic class Solution {public static void main (String args[]) {ObjectOutputStream o Bjectos = null; Serializabletest myTest = new Serializabletest ("str", 1, "123456", 8); try {objectos = new ObjectOutputStream (New Fileo Utputstream ("Test.dat")); Objectos.writeobject (myTest); Objectos.flush (); Objectos.close ();} catch (FileNotFoundException e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Au To-generated catch Blocke.printstacktrace ();} ObjectInputStream Objectin; Serializabletest mts = null;try {objectin = new ObjectInputStream (New FileInputStream ("Test.dat")); MTS = ( serializabletest) Objectin.readobject ();} catch (FileNotFoundException e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Au To-generated catch Blocke.printstacktrace ();} catch (ClassNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();} SYSTEM.OUT.PRINTLN (MTS);//Note that the Equals () method is not overridden, and the object classEquals () defaults to the address comparison System.out.println (Mts.equals (MyTest));}} Class Serializabletest implements Serializable {//serialization ID provides two build strategies under Eclipse//One is a fixed 1L, and one is randomly generating a non-repeating long type of data (actually using JDK tool Generation)//If there is no special requirement, it is to use the default 1L can be static final long serialversionuid = 1L; The string name;int num;static int staticnum;//transient keyword is a transient string pwd;transient int NUM0 that cannot be serialized; Serializabletest (string name, int num, int staticnum, String pwd, int num0) {this.name = Name;this.num = Num;this.staticnu m = staticnum;this.pwd = PWD;THIS.NUM0 = NUM0;} Public String toString () {return "name=" + name + ", num=" + num + ", staticnum=" + Staticnum + ", pwd=" + pwd + ", num0=" + N UM0;}}

Output Result:

Name=str,num=12,staticnum=1,pwd=null,num0=0
False

5. Relationship of pre-and post-serialized objects


After deserializing the restored object address differs from the original address, the address of the object before and after the serialization is different, but the content is the same, and the object contains the same references. In other words, with serialization, we can implement deep copy of any serializable object-meaning we replicate the entire object net, not just the base object and its references. For the same-class objects, their addresses are the same, stating that they are the same object, but not the same as the object addresses of other streams. It is also said that as long as the object is serialized into a single stream, it can recover the same object net as we wrote it, and as long as the object is the same in the same stream.

Serializable interface transient keywords in Java, and object io

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.