"You will recall that today we investigated saving objects to and reading them from a file?"

"Yes, we just saved to an output stream, an read from an input stream."

"Well done, Amigo. It's good to hear that you're noticing these details. Would you be able to complete the code so that it saves to and reads from a file?"

"Complete what?! Declare a FileInputStream and FileOutputStream and pass them to the save and load methods. There's nothing to be confused about here. Super simple."

"I'm happy for you. Now for a new topic: serialization."

Serialization is almost the same as what we just did, but much cooler and built right into the Java machine. The Java machine can store and load its objects. It doesn't even need the save and load methods to do it: All objects are stored inside the Java machine, and it has full access to them."

We just take the object and save it to a stream and read from a stream:

public static void main(String[] args) throws Exception
 Cat cat = new Cat();

 //Save a cat to file
 FileOutputStream fileOutput = new FileOutputStream("cat.dat");
 ObjectOutputStream outputStream = new ObjectOutputStream(fileOutput);

 //Load a cat from file
 FileInputStream fiStream = new FileInputStream("cat.dat");
 ObjectInputStream objectStream = new ObjectInputStream(fiStream);
 Object object = objectStream.readObject();

 Cat newCat = (Cat)object;

"That's it?"

"Exactly. There's a very large and complex serialization mechanism that lets us save to a stream and read from a stream of almost any data type."

"Almost any. So not any data type?"

"Yes, the fact is that not all objects have the inherent ability to be saved. Some objects don't store all their data internally. Instead, they merely reference other objects and/or data sources. For example, the console (System.in), an input stream (InputStream), and other things."

That's why Java's creators came up with the special Serializable interface marker. It's called a marker, because it doesn't contain any data and methods. It's only used to "tag" or "mark" classes. If we believe that our class stores all its data internally, then we can mark it with implements Serializable.

Here's a «cat» example with support for serialization:

class Cat implements Serializable
 public String name;
 public int age;
 public int weight;

When we try to serialize (save) an object, the Java machine checks whether it supports serialization: Does it implement the Serializable interface? If it does, then it saves the object. If not, then it throws an exception to indicate that serialization is impossible.
Here you need to understand that a serializable object must only consist of serializable objects.

"Well, that makes sense. You can't save the whole without saving its parts."


"And what about ints, Strings, and ArrayLists?"

"They all support serialization. Java's creators took special care to make sure this happened. There shouldn't be any problems here."

Moreover, an object's type is saved when the object is serialized. Now you can save a reference to a Cat object in an Object variable. Everything will serialize and deserialize just fine.


"Deserialization is the process of reversing serialization: reading and reconstructing an object from a stream/file."

"Ah, no more questions then."