Java Serialization
In this tutorial, we will learn more and in detail on Java Serialization and Deserialization. Java serialization is very import topic and every developer should know this topic.
Serialization:
- Java Serialization is the process of converting an object’s state to a byte stream and then restoring the object from that byte stream
- The byte stream can be stored in a file, sent over a network, or used for other purposes.
- process of writing state of an Object to file is called Serialization and writing file back to Object is Deserialization.
java.io.Serializable
is Marker Interface- Serialization is performed by the Java Serialization API, which provides a set of classes and methods for serializing and deserializing objects.
- Classes that implement this interface are serializable, meaning their state can be converted to a byte stream and restored from that byte stream.
- Java Serialization API serializes all non-static and non-transient fields of a serializable object
- Custom serialization process involves in implementing the
readObject
andwriteObject
methods - We can control Serialization by using the
transient
keyword to exclude fields from serialization,
Deserialization:
- Deserialization is the process of converting a stream of bytes representing an object into an instance of that object. This is the opposite of serialization.
Deserialization is commonly used in Java for reading objects from a file or over a network. The java.io
package provides the ObjectInputStream
class, which can be used to deserialize objects in Java.
Java code for serializing and deserializing an object:
import java.io.*;
// Jayaram Code
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private int age;
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public static void main(String[] args) {
Employee employee = new Employee("Javasavvy", 30);
// Serializing the object
try (FileOutputStream fileOut = new FileOutputStream("employee.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut)) {
out.writeObject(employee);
} catch (IOException e) {
e.printStackTrace();
}
// Deserializing the object
try (FileInputStream fileIn = new FileInputStream("employee.ser");
ObjectInputStream in = new ObjectInputStream(fileIn)) {
Employee deserializedEmployee = (Employee) in.readObject();
System.out.println("Name: " + deserializedEmployee.getName());
System.out.println("Age: " + deserializedEmployee.getAge());
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
In the above example, Employee
class implements Serializable
interface, which marked that this object can be serializable. serialVersionUID
field is explicitly set to ensure compatibility between different versions of the class.
The main
method serializes the Employee
object to a file called employee.ser
using a FileOutputStream
and an ObjectOutputStream
. The ObjectOutputStream
is used to write the object to the FileOutputStream
.
The main
method also deserializes the Employee
object from the employee.ser
file using a FileInputStream
and an ObjectInputStream
. The ObjectInputStream
is used to read the object from the FileInputStream
, and the object is cast to an Employee
object.
The output of this program will be:
codeName: Javasavvy
Age: 30
Thanks for Reading…