Java Reference
In-Depth Information
Object Serialization
As you learned yesterday during Day 15, “Working with Input and Output,” Java handles
access to external data via the use of a class of objects called streams. A stream is an
object that carries data from one place to another. Some streams carry information from a
source into a Java program. Others go the opposite direction and take data from a pro-
gram to a destination.
A stream that reads a web page's data into an array in a Java program is an example of
the former. A stream that writes a String array to a disk file is an example of the latter.
Two types of streams were introduced during Day 15:
Byte streams , which read and write a series of integer values ranging from 0 to 255
n
Character streams , which read and write textual data
n
These streams separate the data from the Java class that works with it. To use the data at
a later time, you must read it in through a stream and convert it into a form the class can
use, such as a series of primitive data types or objects.
A third type of stream, an object stream , makes it possible for data to be represented as
objects rather than some external form.
Object streams, like byte and character streams, are part of the java.io package.
Working with them requires many of the same techniques you used during Day 15.
For an object to be saved to a destination such as a disk file, it must be converted to ser-
ial form.
NOTE
Serial data is sent one element at a time, like a line of cars on an
assembly line. You might be familiar with the serial port on a com-
puter, which is used to send information as a series of bits one
after the other. Another way to send data is in parallel , transferring
more than one element simultaneously.
An object indicates that it can be used with streams by implementing the Serializable
interface. This interface, which is part of the java.io package, differs from other inter-
faces with which you have worked; it does not contain any methods that must be
included in the classes that implement it. The sole purpose of the Serializable interface
is to indicate that objects of that class can be stored and retrieved in serial form.
 
Search WWH ::




Custom Search