top of page
Writer's pictureThe Tech Platform

What is Serialization in Java?

Serialization is a fundamental concept in Java which help you to manage the data. It offers various benefits such as it preserves object state, facilitate data sharing and communication, supports distributed computing and simplifies the process of saving and loading application states.


Understanding serialization and its applications authorizes developers to effectively manage and manipulate data, enabling robust and flexible Java applications. In this article, we will explore what is serialization in Java, how it works, and why it holds significant importance in Java programming.


Table of content:

What is Serialization in Java?

Serialization in Java refers to the process of converting an object into a stream of bytes so that it can be stored in a file, sent over a network, or saved in persistent storage. The serialized object can later be deserialized, meaning it can be reconstructed back into its original form from the byte stream. This mechanism allows objects to be easily transmitted and recreated across different platforms and environments.

what is serialization in Java

Serialization objects in Java

To serialize an object, it must be written to an output stream using the ObjectOutputStream class, where the writeObject() method handles the conversion. During serialization, the entire object, along with any other objects it references, is processed and converted into bytes. This recursive serialization ensures that the complete object graph is captured in the serialized form.


Here we have an example that explains the serialization process in Java using the ObjectOutputStream class and its writeObject() method:

import java.io.*;

// A simple class to serialize
class Person implements Serializable {
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

public class SerializationExample {
    public static void main(String[] args) {
        // Creating a Person object
        Person person = new Person("John Doe", 30);

        try {
            // Creating an ObjectOutputStream to write the object to a file
            ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("person.ser"));

            // Serializing the object
            outputStream.writeObject(person);

            // Closing the output stream
            outputStream.close();

            System.out.println("Object serialized successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In the above example, we have a simple Person class that implements the Serializable interface. We create a Person object named person with the name "John Doe" and age 30.


To serialize the person object, we create an instance of ObjectOutputStream and pass a FileOutputStream with the desired file name ("person.ser") to its constructor. Then, we call the writeObject() method on the ObjectOutputStream instance, passing in the person object.


During serialization, the writeObject() method automatically converts the object's state into a stream of bytes. It traverses the entire object graph, meaning that if the Person object contained references to other serializable objects, those objects would also be serialized recursively.


Upon successful execution, you will see the message "Object serialized successfully."


Output: The serialization process will create a file named "person.ser" in the same directory as your program. This file will contain the serialized representation of the person object in binary format.


Deserializing Objects in Java:

Deserialization is the process of recreating objects from a stream of bytes. In Java, this is done using the ObjectInputStream class. The readObject() method of this class reads the byte stream and rebuilds the object, along with any other objects it references. This process ensures that the complete object graph is reconstructed, allowing you to work with the objects as if they were in their original form.


Here's an example that will explain the concept of deserialization process in Java using the ObjectInputStream class and its readObject() method:

import java.io.*;

// A simple class to deserialize
class Person implements Serializable {
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

public class DeserializationExample {
    public static void main(String[] args) {
        try {
            // Creating an ObjectInputStream to read the serialized object from the file
            ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("person.ser"));

            // Deserializing the object
            Person person = (Person) inputStream.readObject();

            // Closing the input stream
            inputStream.close();

            System.out.println("Object deserialized successfully:");
            System.out.println("Name: " + person.name);
            System.out.println("Age: " + person.age);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

In the above example, we have the same Person class as before, which implements the Serializable interface.


To deserialize the person object, we create an instance of ObjectInputStream and pass a FileInputStream with the file name ("person.ser") to its constructor. Then, we call the readObject() method on the ObjectInputStream instance and cast the returned object to the Person class.


The readObject() method reads the byte stream from the file and reconstructs the object along with its entire object graph. In this case, it reconstructs the person object with its name and age fields.


Upon successful execution, you will see the deserialized object's details printed in the console.


Output: The program will output the following:

Object deserialized successfully:
Name: John Doe
Age: 30

This confirms that the deserialization process successfully reconstructed the person object from the serialized data stored in the "person.ser" file.


Serializable Interface:

The Serializable interface in Java is a marker interface, meaning it doesn't contain any methods to implement. Its purpose is to serve as a flag or identifier that indicates a class can be serialized.


By implementing the Serializable interface, a class is essentially telling the Java runtime environment that its instances can be converted into a byte stream for serialization.

import java.io.Serializable;

class MyClass implements Serializable 
{
    // Class implementation
}

When a class implements Serializable, the default behavior is to serialize all non-static and non-transient fields of that class. Non-static fields are instance-specific variables, while transient fields are variables that should not be serialized and therefore excluded from the serialization process.

import java.io.Serializable;

class Person implements Serializable 
{
    String name; // Non-static field, will be serialized
    transient int age; // Transient field, will be excluded from serialization
    static String country; // Static field, will be excluded from serialization
    // Rest of the class implementation
}

Fields marked as transient in a Serializable class are not persisted during serialization. This can be useful for sensitive data or for fields that don't need to be stored permanently or transported along with the object.

import java.io.Serializable;

class Person implements Serializable 
{
    String name; // Non-transient field, will be serialized
    transient int age; // Transient field, will be excluded from serialization
    // Rest of the class implementation
}

To exclude a field from serialization, you can simply declare it as transient in your class. When the object is serialized, the value of the transient field will not be saved or transmitted. Upon deserialization, the transient field will be initialized with the default value for its data type.

import java.io.Serializable;

class Person implements Serializable 
{
    String name; // Non-transient field, will be serialized
    transient int age; // Transient field, will be excluded from serialization
    // Rest of the class implementation
}

By default, all non-static and non-transient fields are serialized. However, marking a field as transient explicitly excludes it from the serialization process. This allows you to control which fields are serialized and which are not, giving you greater flexibility and control over the serialization of your objects.


Benefits of Serialization in Java

Serialization offers several benefits in Java:

  1. Serialization allows objects to be saved to a file or database and later retrieved, ensuring that their states are preserved between different program executions.

  2. Serialized objects can be transmitted over a network or shared between different systems, enabling seamless data exchange and communication.

  3. Serialization is crucial for distributed computing environments where objects need to be sent across network boundaries. It enables remote method invocation, allowing objects to be invoked remotely as if they were local.

  4. Serialization simplifies the task of saving and loading the state of an application. By serializing objects representing the application state, it becomes easier to store and restore the entire application's state when needed.


Disadvantages of Serialization in Java

While serialization offers several advantages, there are also some potential disadvantages to consider when using serialization in Java:

  1. Serialization adds additional processing overhead compared to direct access to object data. The process of converting objects into a byte stream and vice versa can be resource-intensive and impact performance, especially for large or complex object graphs.

  2. When changes are made to a serialized class's structure or fields, compatibility issues may arise during deserialization. Incompatible changes can result in exceptions or data corruption, requiring careful handling of versioning to ensure smooth compatibility between serialized data and class versions.

  3. Serialization poses security risks, particularly when deserializing objects from untrusted sources. Deserialization of maliciously crafted objects can lead to security vulnerabilities, such as remote code execution or denial of service attacks. Proper precautions, such as input validation and using secure serialization techniques, are necessary to mitigate these risks.

  4. Serialized data is typically stored in binary format, making it difficult for humans to interpret or modify directly. This lack of human readability can be a disadvantage when troubleshooting, debugging, or performing manual data manipulation.


Conclusion

Serialization plays a vital role in Java programming by enabling the storage, transmission, and reconstruction of object states. By implementing the Serializable interface and utilizing the ObjectInputStream and ObjectOutputStream classes, developers can easily serialize and deserialize objects in Java. Understanding the concepts of serialization, the transient keyword, and the important methods associated with it, such as writeObject() and readObject(), empowers developers to effectively manage object persistence and data transportability in their Java applications.

0 comments

Recent Posts

See All

Comments


bottom of page