Serialization and deserialization are important concepts in Java that allow objects to be converted into a stream of bytes and then reconstructed back into objects. This process is useful for various purposes, such as storing objects in files or sending them over a network.
- Serialization: Serialization is the process of converting an object into a byte stream. To make an object serializable in Java, the class must implement the
java.io.Serializable
interface. This interface acts as a marker, indicating that the class can be serialized. By default, all of its instance variables are also serialized unless marked with thetransient
keyword.
Here’s an example of a serializable class:
import java.io.Serializable; public class Person implements Serializable { private String name; private int age; // Constructor, getters, and setters @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } }
- Serialization Process: To serialize an object, you need to use the
java.io.ObjectOutputStream
class. Here’s an example of how to serialize aPerson
object:
import java.io.FileOutputStream; import java.io.ObjectOutputStream; import java.io.IOException; 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 object is saved in person.ser"); } catch (IOException e) { e.printStackTrace(); } } }
In the example above, the Person
object is serialized and saved in the “person.ser” file using ObjectOutputStream.writeObject()
.
- Deserialization: Deserialization is the reverse process of serialization. It involves converting a byte stream back into an object. To deserialize an object, the class must implement the
java.io.Serializable
interface.
Here’s an example of how to deserialize a Person
object:
import java.io.FileInputStream; import java.io.ObjectInputStream; import java.io.IOException; public class DeserializationExample { public static void main(String[] args) { try (FileInputStream fileIn = new FileInputStream("person.ser"); ObjectInputStream in = new ObjectInputStream(fileIn)) { Person person = (Person) in.readObject(); System.out.println("Deserialized object: " + person); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } }
In the example above, the “person.ser” file is read using FileInputStream
, and then the object is deserialized using ObjectInputStream.readObject()
.
It’s important to note that during deserialization, the class of the serialized object must be available on the classpath. Otherwise, a ClassNotFoundException
will be thrown.
That’s the basic idea of serialization and deserialization in Java. It allows you to convert objects into a serialized form and back again, enabling various use cases like object persistence and inter-process communication.