Jump to content

Java serialization

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Zanerock (talk | contribs) at 18:00, 27 November 2003. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

Serialization is the process of taking an in memory representation of an object and encoding it using a standard format. This encoded version can then be saved to disk, sent across a network connection, or otherwise communicated to another Java virtual machine (or perhaps the same JVM at a different time.

Serialization is used as a simple way to pesist objects, send messages, or even to distribute objects. There are certainly other ways to accomplish these things, and serialization is often viewed as a quick and dirty method. There are cases, however, where it is perfectly adequate to the task at hand.

Java provides automatic serialization which only requires that the object me marked by implementing the Serializable interface. For some not-to-clear reason, the process of serialization is handled in a very idiosyntric manner. There are no serialization methods defined on the Serializable interface; implementing the interface just marks the class as "okay to serialize." Java then handles serialization internally, though it does allow the developer to override this by implementing two special methods on a Serializable object. Again, these methods are not defined on the Serializable interface.

The standard encoding method uses a simple transaltion of the fields into a byte stream. Primitives as well as referenced objects are encoded into the stream. Each object that is referenced by the serialized object must also be serialized and if any in the reference graph is not Serializable, then serialization will fail (unless the developer has redefined the serialization for an object and truncates some portion of the reference graph).

There is a push to redefine, or provide an alternative to the standard serialization protocol that uses XML and produces a human readable encoding. Such an encoding could be useful for persisted objects that may be browsed by humans or communicated to other, non-Java systems, but the more compact, byte encoding is generally more practical.