Java Serialization and Deserialization – Complete Guide with Examples
Learn how to serialize Java objects into byte streams and deserialize them back, including implementation, transient fields, and versioning for efficient object storage and transfer.
Serialization and Deserialization in Java – Complete Detailed Tutorial
Serialization in Java allows you to convert objects into a byte stream for storage or transmission.
Deserialization is the reverse process – converting the byte stream back into a Java object.
These concepts are part of java.io package.
1. Why Serialization is Used
- Persist objects to files or databases
- Send objects over networks (RMI, sockets)
- Store objects in cache
- Clone objects or maintain object state
2. Serializable Interface
- Marker interface (no methods)
- Classes implementing
Serializablecan be serialized
import java.io.Serializable;
class Student implements Serializable {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return name + " (" + age + ")";
}
}
Key Points:
- Implements java.io.Serializable
- No methods to override
- All fields are serialized by default
3. Serializing Objects
- Use ObjectOutputStream with FileOutputStream
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class Main {
public static void main(String[] args) {
Student student = new Student("Alice", 22);
try (FileOutputStream fos = new FileOutputStream("student.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos)) {
oos.writeObject(student); // serialize object
System.out.println("Student object serialized successfully!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Explanation:
- Object is converted into a byte stream
- Saved in student.ser file
4. Deserializing Objects
- Use ObjectInputStream with FileInputStream
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
public class Main {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream("student.ser");
ObjectInputStream ois = new ObjectInputStream(fis)) {
Student student = (Student) ois.readObject(); // deserialize object
System.out.println("Deserialized Student: " + student);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
Output:
Deserialized Student: Alice (22)
5. Transient Fields
- Fields declared as
transientare not serialized
class Student implements Serializable {
private String name;
private transient int age; // will not be serialized
public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
- During deserialization,
agewill be default value (0)
6. serialVersionUID
- Unique identifier for class versioning
- Helps maintain compatibility during deserialization
private static final long serialVersionUID = 1L;
- If class structure changes, it prevents InvalidClassException
7. Key Points
- Serialization converts object → byte stream
- Deserialization converts byte stream → object
- Use transient for non-serializable fields
- Always define serialVersionUID for version control
- Implements Serializable interface (marker interface)
8. Summary
- Java provides ObjectOutputStream and ObjectInputStream
- Essential for persistent storage and network communication
- Works with custom objects safely
- Core concept for Java I/O Streams