Serialization And Deserialization In Java

Basic of Serialization and Deserialization in Java Language

Serialization And Deserialization In Java

I made a switch to Java in January this year after I got an Internship with Flexisaf Edusoft. Prior to that time I basically wrote PHP and a bit of Javascript. It was a well-structured internship and my first.

The serialization was a topic I never came across in PHP throughout my usage of the language. However, serialization actually existed in PHP just that I never came across it, but it is more in use in Java than it's in PHP. Today I will introduce serialization and deserialization to you and a few ways it has been applied in Java.

WHAT IS SERIALIZATION/DESERIALIZATION

Serialization simply means converting an object from a class into a byte state in a Java virtual machine to be transferred to another Java virtual machine that recreates the object from the byte state and the process of recreating the object is referred to as Deserialization.

Example of serialization and deserialization Serialization Let's create a class whose object will be serialized.

import java.io.*;

public class Person implements Serializable{

    int id = 0;
    String name = "empty";

    public Person(int identity, String nomenclature) {

        name = nomenclature;
        id = identity;
    }
}

The class Person implements Serializable to enable its object to be serialized/deserialized. Person class has two fields id and name; that change from default value upon class instantiation. Java.io package where the Serializable interface and other classes used in the program were imported.

public static void main(String[] args) throws FileNotFoundException, IOException {

        String filename = "filename here";
        Person person = new Person(1, "John");

        // serialization
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filename));

        try {

            out.writeObject(person);
            System.out.println("Success");
        } catch(Exception e) {

            System.out.println("Unsuccessful");
} finally {

            if(out != null) {

                out.close();
            }
        }
}

The main method as you know runs the serialization and prints out success else prints unsuccessful. We use the ObjectOutputStream and its method writeObject to serialize objects.

Deserialization

public static void main(String[] args) throws FileNotFoundException, IOException {

        String filename = "filename here";
        Person person = new Person(1, "John");

 // Deserialization
        ObjectInputStream in = new ObjectInputStream(new FileInputStream(filename));

        try {

            Person personObj = (Person)in.readObject();
            System.out.println("Person Id is " +personObj.id + " while name is " + personObj.name);
        } catch (Exception e) {

            e.printStackTrace();
        } finally {

            if(in != null) {

                in.close();
            }
        }
}

Deserialization is the reverse it uses the ObjectInputStream and the readObject method to reconstruct the object from the byte state. You will notice that it was cast to a Person data type this is to enable access to the fields in the Person class.

An object of a class that does not implement serialize interface cannot be serialized and any class that references a class that implements serialize interface must itself implement the serialize interface or else an exception will be thrown.

Serialization is platform-independent i.e serialize byte stream can be deserialized by a different Java Virtual Machine.

Static and transient fields are not serialized, so if you have a field you do not want to be serialized make it transient or static. In the case of static, it is not serialized because the static field is owned by the class, not the object while transient prevents the field from being serialized. Serialization is applied in Hibernate, JPA, and RMI.

Serialization can be customized but this is beyond the scope of this article you can read more on custom serialization and how to implement it or I may write something on it sometime.

I hope you understood what serialization/deserialization means, thank you for your time. Have an impactful day ahead.

if you still have any questions drop them in the comment section or reach me on LinkedIn.

Credits

Naresh Joshi

Javatpoint