Techoceanhub logo

Serialization and deserialization

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.

  1. 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 the transient 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 + "]";
    }
}

  1. Serialization Process: To serialize an object, you need to use the java.io.ObjectOutputStream class. Here’s an example of how to serialize a Person 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().

  1. 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.

Share:

Categories

Archives

Related Posts

Array Destructuring

Array Destructuring Array destructuring is a powerful syntax introduced in ES6 that allows you to unpack elements from an array

Read More »

Explore our Affiliated partner links

hostgator website plan

Hostgator

Best Website hosting plans & Best for 24/7 support.

hostinger webhosting plan

Hostinger

Reasonable website hosting packages for small business & 24/7 Email support

Elementor pro

Design Worpdress website quickly using Elementor Builder and AI

buy elementor website templates

Buy Elementor Website Template

Buy ready made Elementor Website Template from us & Get Free Elementor pro licence for 1 year .