Deserialization in Java is the process of converting a byte stream back into a live Java object. It is the reverse of serialization and is commonly used when reading objects from files, databases, or network streams.
ObjectInputStreamSerializableDuring deserialization, the JVM recreates the object without calling constructors. The class definition must be available in the classpath for successful deserialization.
// Deserializing an object from a file
import java.io.*;
class Student implements Serializable {
String name;
int age;
}
public class DeserializeDemo {
public static void main(String[] args) throws Exception {
ObjectInputStream in=new ObjectInputStream(new FileInputStream("student.ser"));
Student s=(Student)in.readObject();
in.close();
System.out.println(s.name+" "+s.age);
}
}
Simulate the cycle to see how the transient keyword affects deserialization.
The program reads the serialized object from student.ser and restores the Student object with its original values.
serialVersionUID during serialization and deserialization