3.2.5.1 Object Serialization3.2.5 Binary Input and Output3.2.5 Binary Input and Output3.2.5.2 Binary Output

3.2.5.1 Object Serialization

Assume that we have a class

  public class Tuple implements Serializable
  {
    Integer first;
    String second;
    public Tuple(Integer f, String s)
    {
      first = f;
      second = s;
    }
  }

whose objects hold pairs of an Integer and a String object. A class that may be written to disk must implement the interface Serializable; this interface does not include any methods but just serves as a flag to the compiler to signal that objects of this type may be serialized. The fields of such an object must only have object types that also implement Serializable; this is the case for many classes in the Java standard library (such as Integer or String).

We may create a Tuple object as

  Tuple tuple = new Tuple(new Integer(1), "one");

Please note that tuple refers via its fields to two other objects; one Integer object that holds an int value 1 and a String object "one". Our goal is to write this object including the referenced objects to disk and to restore this object from disk again.


© Wolfgang Schreiner; February 3, 2005

3.2.5.1 Object Serialization3.2.5 Binary Input and Output3.2.5 Binary Input and Output3.2.5.2 Binary Output