serialization of Java objects
For an object that exists in a Java virtual machine, its internal state is only persisted in memory. After the JVM exits, the memory resources are freed and the internal state of the Java object is lost. In many cases, the internal state of the object needs to be persisted, saving the state of the object in operation (the most straightforward way is to save it to the file system), and can be restored when needed, even in the case of a Java virtual machine exiting.
The object serialization mechanism is an object persistence method built in Java, which can easily be transformed between the active object in the JVM and the byte array (stream), the Java object can be stored, can be transmitted by the network, the object is serialized into a byte stream at one end of the network, and transmitted over the network to the other end of the network. , you can re-restore from a byte stream to an object in the running state of the Java Virtual machine.
1. Related Interfaces
The serialization of objects in Java classes is done through ObjectOutputStream and ObjectInputStream.
- ObjectOutputStream (outputstream out);
- void writeobject (object obj);
-
- objectinputstream (inputstream in);
- object readobject (); //reads the restored object information from the specified stream
can only read and write objects using the ReadObject () |writeobject () method. In addition to objects, basic types and arrays in Java can also be serialized, and for basic types, they can be read and written using Readint (), Writeint (), ,
readdouble (), writedouble (), and similar interfaces.
2. Serializable interface
for any object that needs to be serialized, you must implement the interface serializable, which is just an identity interface. There is no member of itself, just the object that identifies the current implementation class can be serialized.
3. transient keyword
If there are some properties in the class that you want to not be serialized during object serialization, Use the keyword transient to label adornments. When an object is serialized, the member property labeled transient is automatically skipped.
4.Java serialization requires attention:
(1). When an object is serialized, only non-static member variables of the object are saved, and no member methods, static member variables (but not errors), and transient dimension member variables are saved.
(2). If the member variable of an object is an object, the data members of the object will be saved and restored, and will be recursive. (The class that is the object of the member variable must also implement the Serializable interface)
(3). If a serializable object contains a reference to an object that is not serializable, the entire serialization operation will fail. and throws a notserializableexception. The reference tag can be transient, and the object can still be serialized.
Http://www.cnblogs.com/wangjiyuan/p/3487778.html
Considerations for serialization/deserialization of Java Objects (GO)