Serialization is the process of writing the state of an object to a byte stream. This is useful when you want to save the state of your program to a persistent storage area, such as a file. At a later time, you may restore these objects by using the process of deserialization.
Serialization is also needed to implement Remote Method Invocation (RMI). RMI allows a Java object on one machine to invoke a method of a Java object on a different machine. An object may be supplied as an argument to that remote method.
The sending machine serializes the object and transmits it. The receiving machine deserializes it.
Example:
Assume that an object to be serialized has references to other objects, which, in turn, have references to still more objects.
This set of objects and the relationships among them form a directed graph. There may also be circular references within this object graph.
That is, object X may contain a reference to object Y, and object Y may contain a reference back to object X. Objects may also contain references to themselves.
The object serialization and deserialization facilities have been designed to work correctly in these scenarios.
If you attempt to serialize an object at the top of an object graph, all of the other referenced objects are recursively located and serialized. Similarly, during the process of deserialization, all of these objects and their references are correctly restored.
0 Comments