Java Reference
In-Depth Information
20.8. Object Serialization
The ability to save objects in a byte stream that can be transferred across
the network (perhaps for use in remote method invocations), saved to
disk in a file or database, and later reconstituted to form a live object, is
an essential aspect of many real-world applications.
The process of converting an object's representation into a stream of
bytes is known as
serialization,
while reconstituting an object from a byte
stream is
deserialization.
When talking about the classes, interfaces, and
language features involved in this overall process, we generally just use
the term
serialization
and understand that it includes deserialization as
well.
A number of classes and interfaces are involved with serialization. You
have already learned about the basic mechanisms for reading and writing
primitive types and strings using the
Data
stream classes (see page
ObjectOutputStream
that allow you to serialize and deserialize complete ob-
jects. Various other classes and interfaces provide specific support for the
serialization process. In addition, the field modifier
TRansient
provides a
language-level means of marking data that should not be serialized.
20.8.1. The
Object
Byte Streams
The
Object
streams
ObjectInputStream
and
ObjectOutputStream
allow you to
read and write object graphs in addition to the well-known types (primit-
ives, strings, and arrays). By "object graph" we mean that when you use
writeObject
to write an object to an
ObjectOutputStream
, bytes represent-
ing the objectincluding all other objects that it referencesare written to
the stream. This process of transforming an object into a stream of bytes
is called
serialization.
Because the serialized form is expressed in bytes,
not characters, the
Object
streams have no
Reader
or
Writer
forms.