serialization in java
Serialization in Java is a mechanism provided by the Java language to convert an object into a byte stream, which can then be reverted back into a copy of the object (deserialization). This process is essential for various tasks, such as persisting object states, transferring objects over a network, or saving objects to a file.

Table of Contents
Key Concepts:
- 1. Serialization:
- Definition: The process of converting an object into a byte stream.
- Purpose: To save the state of an object so it can be recreated later.
- Usage: Commonly used for saving objects to files, sending objects over a network, or storing objects in databases.
- 2. Deserialization:
- Definition: The process of converting a byte stream back into a copy of the object.
- Purpose: To restore the state of an object from its serialized form.
- Usage: Used to read objects from files, receive objects over a network, or retrieve objects from databases.
How Serialization Works in Java:
- 1. Serializable Interface:
- To serialize an object, the class of the object must implement the `java.io.Serializable` interface.
- The `Serializable` interface is a marker interface (an interface with no methods), which indicates that the class is eligible for serialization.
- 2. ObjectOutputStream and ObjectInputStream:
- `ObjectOutputStream`: Used to write an object to an OutputStream.
- `ObjectInputStream`: Used to read an object from an InputStream.
Serializing an Object
```java
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Person implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Getters and setters
}
public class SerializationExample {
public static void main(String[] args) {
Person person = new Person("John Doe", 30);
try (FileOutputStream fileOut = new FileOutputStream("person.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut)) {
out.writeObject(person);
System.out.println("Serialized data is saved in person.ser");
} catch (IOException i) {
i.printStackTrace();
}
}
}
```
Deserializing an Object
```java
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
public class DeserializationExample {
public static void main(String[] args) {
Person person = null;
try (FileInputStream fileIn = new FileInputStream("person.ser");
ObjectInputStream in = new ObjectInputStream(fileIn)) {
person = (Person) in.readObject();
System.out.println("Deserialized Person:");
System.out.println("Name: " + person.getName());
System.out.println("Age: " + person.getAge());
} catch (IOException i) {
i.printStackTrace();
} catch (ClassNotFoundException c) {
System.out.println("Person class not found");
c.printStackTrace();
}
}
}
```
Important Points:
- 1. serialVersionUID:
- A unique identifier for each class.
- Used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization.
- If the receiver has loaded a class for the object that has a different `serialVersionUID` than that of the corresponding sender’s class, deserialization will result in an `InvalidClassException`.
- 2. Transient Fields:
- Fields marked with the `transient` keyword are not serialized.
- Useful for excluding fields that are not serializable or that should not be saved, such as sensitive information.
- 3. Serializable and Inheritance:
- If a superclass implements Serializable, then the subclass automatically does so.
- If a superclass does not implement Serializable, then the subclass must explicitly implement it to be serializable.
- 4. Custom Serialization:
- Classes can define custom serialization behavior by implementing `writeObject` and `readObject` methods.
- These methods allow control over the serialization and deserialization processes, enabling handling of complex object states and transient fields.
Serialization is a powerful feature in Java that facilitates the persistence and transfer of objects, making it crucial for many Java applications.