I. Definitions and related concepts
The emergence of the internet has brought about the need for inter-machine communication, and both parties to the interconnected communication are required to adopt agreed protocols, which are part of the communication Protocol. Communication protocols often adopt a layered model, each layer of different models of functional definition and granularity, for example: TCP/IP protocol is a four-layer protocol, and the OSI model is a Seven-layer protocol model. The main function of the Presentation layer in the OSI Seven layer protocol model is to convert the object of the application layer into a contiguous binary string, or vice versa, to convert the binary string into an application-level Object-two functions are serialization and Deserialization. In general, the application layer of the TCP/IP protocol corresponds to the application layer, the presentation layer, and the session layer of the OSI Seven layer protocol model, so the serialization protocol is part of the application layer of the TCP/IP Protocol.
- Serialization: the process of converting a data structure or object into a binary string
- Deserialization: the process of converting a binary string that is generated during serialization into a data structure or object
- The translation of binary data and objects is, frankly,
Data structures, objects, and binary strings
In different computer languages, data structures, objects, and binary strings are not represented in the same way.
Data structures and objects: for a completely object-oriented language like java, everything that an engineer does is object, from the instantiation of the class. The closest concept to data structures in the Java language is Pojo (Plain old Java Object) or javabean--those classes that have only setter/getter methods. In the semi-object-oriented language of C + +, the data structure and struct correspond to the object and class.
Binary string: serialization generates a binary string that refers to a piece of data stored in Memory. The C + + language has a memory operator, so the concept of a binary string is easy to understand, for example, the C + + language string can be used directly by the transport layer because it is essentially a binary string stored in memory that ends with '/'. In the Java language, the concept of a binary string is easily confused with a string. In fact, string is a class-one citizen of Java and is a special object. For Cross-language communication, The serialized data cannot, of course, be a special data type for a particular language. The binary string in Java refers to Byte[],byte as one of the 8 native data types in Java (Primitive data types).
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;
5. Serialization ID
The serialization ID provides two build strategies under Eclipse, One fixed 1L, one randomly generating a non-repeating long type of data (actually generated using the JDK tool), where there is a recommendation that, if there is no special requirement, it is available with the default 1L, which ensures that the code is consistent When deserialization succeeds. This can also be the cause of serialization and deserialization failures because different serialization IDs cannot be serialized and Deserialized.
6. Relationship of Pre-and post-serialized objects
Is it "= =" or equal? Or is it a shallow copy or a deep copy?
Answer: Deep copy, Deserialize the restored object address is different from the original address
The address of the object before and after 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.
7. Can the static variable be serialized
If you add static to the age variable in the preceding code, the output is still
Name=sheepmu
Age=24
But look at the following example:
?
| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
package com.sheepmu;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;public class MyTest implements Serializable{ private static final long serialVersionUID = 1L; private String name="SheepMu"; private static int age=24; public static void main(String[] args) {//以下代码实现序列化 try { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("my.out"));//输出流保存的文件名为 my.out ;ObjectOutputStream能把Object输出成Byte流 MyTest myTest=new MyTest(); oos.writeObject(myTest); oos.flush(); //缓冲流 oos.close(); //关闭流 } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } fan();//调用下面的 反序列化 代码 } public static void fan() { new MyTest().name="SheepMu_1";//!!!!!!!!!!!!!!!!重点看这两行 更改部分 age=1;<span style="font-family: verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif; ">//!!!!!!!!!!!!!!!!!!!重点看这两行 更改部分</span> ObjectInputStream oin = null;//局部变量必须要初始化 try { oin = new ObjectInputStream(new FileInputStream("my.out")); } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } MyTest mts = null; try { mts = (MyTest ) oin.readObject();//由Object对象向下转型为MyTest对象 } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println("name="+mts.name); System.out.println("age="+mts.age); }} |
The output is:
Name=sheepmu
Age=1
Why is the age variable of the top code added to static or deserialized by 24? The new code that assigns a value to a variable, not static, gets the value of the serialization itself, while Static gets the new Value. Cause: serialization ignores static variables, that is, serialization does not save the state of static Variables. static members are class-level, so they cannot be serialized. That is, serializing the state of the object is not the state of the class. The meaning of this cannot be serialized is that the serialized information does not contain this static member Field. When static is added to the top, it is still output 24 because the value is the value assigned when the JVM loads the class. Note:the variable after transient cannot be serialized , but the situation is slightly more complicated, the opening point Later.
8. Summary:
A) when a parent class is serialized and the subclass is automatically serialized, no explicit implementation of the serializable interface is required;
B) when an instance variable of an object refers to another object, the object is serialized by serializing it;
C) the variable after static,transient cannot be serialized;
second, the serialization of Protocol characteristics
Each of these serialization protocols has advantages and disadvantages, and they have their own unique application scenarios at the beginning of the DESIGN. In the process of system design, it is necessary to take into account all aspects of serialization requirements, comprehensively compare the characteristics of various serialization protocols, and finally give a compromise scheme.
Versatility
Versatility has a two-level meaning:
first, the technical level, whether the serialization protocol supports cross-platform, Cross-language. If not supported, the versatility on the technical level is greatly reduced.
second, popularity, serialization and deserialization require multi-stakeholder engagement, and seldom-used protocols often mean expensive learning costs; on the other hand, low-prevalence protocols often lack a stable and mature cross-lingual, cross-platform public package.
This paper mainly refer to http://www.2cto.com/kf/201405/305380.html
http://kb.cnblogs.com/page/515982/
Thanks to the author
Serialization and deserialization of Java