- http://javarevisited.blogspot.com/2011/04/top-10-java-serialization-interview.html
- http://www.javaworld.com/javaworld/jw-07-2000/jw-0714-flatten.html
- http://www.javaworld.com/community/node/2915
Serialization is Java's method of writing an object to a file and then reading it back. It is used for object persistence. You can implement the serialization interface and implement the write and the read classes. Static members belong to the class and not to the object and cannot be serialized. Hence, you use separate methods for them. If you have some members that you don't want to save during serialization, you should keep it as transient. Besides, if there are some transient properties in your object, then you should not write it directly. You should only write the non-transient and non-static properties separately.
- Object serializationallows an object to be transformed into a sequence of bytes that
can later be re-created (deserialized) into the original object. After deserialization,
the object has the same state as it had when it was serialized, barring any data
members that were not serializable. This mechanism is generally known as persist-ence. Java provides this facility through the ObjectInputand ObjectOutputinterfaces,
which allow the reading and writing of objects from and to streams. These two
interfaces extend the DataInputand DataOutputinterfaces
- The ObjectOutputStreamclass and the ObjectInputStreamclass implement the
ObjectOutputinterface and the ObjectInputinterface, respectively, providing meth-ods to write and read binary representation of objects as well as Java primitive val-ues.
- For example, in order to store objects in a file and thus provide persistent storage
for objects, an ObjectOutputStreamcan be chained to a FileOutputStream:
FileOutputStream outputFile = new FileOutputStream("obj-storage.dat");
ObjectOutputStream outputStream = new ObjectOutputStream(outputFile);
Objects can be written to the stream using the writeObject()method of the
ObjectOutputStreamclass:
ThewriteObject()method can be used to write anyobject to a stream, including
strings and arrays, as long as the object implements the java.io.Serializableinter-face, which is a marker interface with no methods. The Stringclass, the primitive
wrapper classes and all array types implement the Serializableinterface. A serial-izable object can be any compound object containing references to other objects,
and all constituent objects that are serializable are serialized recursively when the
compound object is written out. This is true even if there are cyclic references
between the objects. Each object is written out only once during serialization. The
following information is included when an object is serialized:
• the class information needed to reconstruct the object.
- the values of all serializable non-transient and non-static members, including
those that are inherited.
- Besides, an object is serializable if its constituent objects are all serializable.
No comments:
Post a Comment