Serialization in Java
Serialization in java is a mechanism of writing the state of an object into a byte stream.
It is mainly used in Hibernate, RMI, JPA, EJB and JMS technologies.
The reverse operation of serialization is called deserialization.
Advantage of Java Serialization
It is mainly used to travel object's state on the network (known as marshaling).
Serializable is a marker interface (has no data member and method). It is used to "mark" java classes so that objects of these classes may get certain capability. The Cloneable and Remote are also marker interfaces.
It must be implemented by the class whose object you want to persist.
The String class and all the wrapper classes implements java.io.Serializable interface by default.
Let's see the example given below:
In the above example, Student class implements Serializable interface. Now its objects can be converted into stream.
The ObjectOutputStream class is used to write primitive data types and Java objects to an OutputStream. Only objects that support the java.io.Serializable interface can be written to streams.
Example of Java Serialization
In this example, we are going to serialize the object of Student class. The writeObject() method of ObjectOutputStream class provides the functionality to serialize the object. We are saving the state of the object in the file named f.txt.
Deserialization in java
Deserialization is the process of reconstructing the object from the serialized state.It is the reverse operation of serialization.
An ObjectInputStream deserializes objects and primitive data written using an ObjectOutputStream.
Example of Java Deserialization
Java Serialization with Inheritance (IS-A Relationship)
If a class implements serializable then all its sub classes will also be serializable. Let's see the example given below:
Now you can serialize the Student class object that extends the Person class which is Serializable.Parent class properties are inherited to subclasses so if parent class is Serializable, subclass would also be.
Java Serialization with Aggregation (HAS-A Relationship)
If a class has a reference of another class, all the references must be Serializable otherwise serialization process will not be performed. In such case, NotSerializableException is thrown at runtime.
Since Address is not Serializable, you can not serialize the instance of Student class.
Note: All the objects within an object must be Serializable.
Java Serialization with static data member
If there is any static data member in a class, it will not be serialized because static is the part of class not object.
Java Serialization with array or collection
Rule: In case of array or collection, all the objects of array or collection must be serializable. If any object is not serialiizable, serialization will be failed.
Externalizable in java
The Externalizable interface provides the facility of writing the state of an object into a byte stream in compress format. It is not a marker interface.
The Externalizable interface provides two methods:
If you don't want to serialize any data member of a class, you can mark it as transient.
Visit next page for more details.