Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 6 of 10 · File Handling and I/O Operations
Interview question

What is Object Serialization in Java and how do you use it? Java में Object Serialization क्या है और कैसे use करते हैं?

Answer

Serialization converts Java objects into byte stream format for storage or transmission. Deserialization reconstructs objects from byte stream. It's essential for persistence and network communication.

import java.io.*;

// Step 1: Make class Serializable
public class Student implements Serializable {
    private static final long serialVersionUID = 1L;
    
    private int id;
    private String name;
    private double gpa;
    
    public Student(int id, String name, double gpa) {
        this.id = id;
        this.name = name;
        this.gpa = gpa;
    }
    
    @Override
    public String toString() {
        return 'Student{' + 'id=' + id + ', name=' + name + 
               ', gpa=' + gpa + '}';
    }
}

// Step 2: Serialize objects
public class SerializationDemo {
    public static void main(String[] args) {
        // Serialization (Object to File)
        try (ObjectOutputStream oos = new ObjectOutputStream(
                new FileOutputStream('student.ser'))) {
            Student s1 = new Student(1, 'John', 3.8);
            Student s2 = new Student(2, 'Jane', 3.9);
            
            oos.writeObject(s1);
            oos.writeObject(s2);
            System.out.println('Objects serialized');
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        // Deserialization (File to Object)
        try (ObjectInputStream ois = new ObjectInputStream(
                new FileInputStream('student.ser'))) {
            Student st1 = (Student) ois.readObject();
            Student st2 = (Student) ois.readObject();
            
            System.out.println(st1);
            System.out.println(st2);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

// Important: serialVersionUID
// Version identifier for serialization
// Change when class structure changes
private static final long serialVersionUID = 1L;

// Transient keyword - exclude field from serialization
public class User implements Serializable {
    private String username;
    private transient String password;  // Not serialized
}

// Collections serialization
List<Student> students = new ArrayList<>();
students.add(new Student(1, 'John', 3.8));

try (ObjectOutputStream oos = new ObjectOutputStream(
        new FileOutputStream('students.ser'))) {
    oos.writeObject(students);
}
Object Serialization:

1. Implement Serializable interface
2. Add serialVersionUID
3. Use ObjectOutputStream to write
4. Use ObjectInputStream to read

Serialization:
try (ObjectOutputStream oos = new ObjectOutputStream(
        new FileOutputStream('file.ser'))) {
    oos.writeObject(object);
}

Deserialization:
try (ObjectInputStream ois = new ObjectInputStream(
        new FileInputStream('file.ser'))) {
    Object obj = ois.readObject();
}

Use Cases: Persistence, network, caching

Was this answer clear?