Java Tutorial - Java Script : Object Serialization

Java Tutorial - Java Script :

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 asource into a Java program. Others go the opposite direction and take data from a program 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
·         Character streams, which read and write textual data

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 serial form.
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 computer, 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 interfaces 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.
Objects can be serialized to disk on a single machine or can be serialized across a network such as the Internet, even in a case in which different operating systems are involved. You can create an object on a Windows machine, serialize it to a Linux machine, and load it back into the original Windows machine without error. Java transparently works with the different formats for saving data on these systems when objects are serialized.
A programming concept involved in object serialization is persistence—the capability of an object to exist and function outside the program that created it. Normally, an object that is not serialized is not persistent. When the program that uses the object stops running, the object ceases to exist.
Serialization enables object persistence because the stored object continues to serve a purpose even when no Java program is running. The stored object contains information that can be restored in a program so that it can resume functioning. When an object is saved to a stream in serial form, all objects to which it contains references also are saved. This makes it easier to work with serialization; you can create one object stream that takes care of numerous objects at the same time. When several objects contain references to the same object, Java automatically ensures that only one copy of that object is serialized. Each object is assigned an internal serial number; successive attempts to save that object store only that number. You can exclude some of an object’s variables from serialization to save disk space or prevent information that presents a security risk from being saved. As you will see later today, this requires the use of the transient modifier.