Serialization in Java converts an object’s state into a byte stream; deserialization is the reverse process. Another way, serialization is converting a Java object into a static stream (sequence) of bytes that we can then preserve in a database or send over the internet. Deserialization is recreating the actual Java object in memory using the byte stream. This means is utilized to keep an object alive.
Before we go into serialization details, let’s look at a real-world example.
Assume you’re on the phone with a buddy via a cellular network. Your voice can be heard by a friend who is thousands of miles distant. You may also be aware that your voice is translated into electrical signals and subsequently transmitted via radio signals to a mobile tower. The tower later channels the electrical signal to the precise phone number you are connected with.
Your voice is converted many times before it reaches your companion on the other side. Similarly, when moving from one Java Virtual Machine to another, Java objects require serialization and deserialization.
he JVM grants the class that implements the Serializable Interface extra capabilities.
A class called “ObjectOutputStream” has a method called “writeObject.” This method is in charge of turning an item into a Byte stream. The class “ObjectInputStream” has a method called readObject (). This method turns a byte stream into a string.
Serializable is an interface that allows data to be serialized (has no data member and method). It’s used to “mark” Java classes so that their objects can have specific capabilities.
For writing objects into streams, this class is required. Only classes that implement the Serializable interface can be used as arguments in this class’s methods. The object is transformed into a byte stream using the methods in this class.
Let’s have a look at constructor function of this class-
This function Object() creates a byte stream that writes to an Object stream.
Now let us glance at the methods inside this class. This class mainly houses three methods.
This class aims to perform the exact opposite of what the ObjectOutputStream class accomplished. Instead, it turns the byte stream into objects that can be deserialized.
Let’s have look at the constructor function of this class-
This constructor is crucial because it produces an ObjectInputStream, which reads from the InputStream.
Let’s move into how to serialize an object now that you know what serialization in Java is and all the important characteristics. For serialization, we have to use the ObjectOutputStream class’s writeObject() function, and for deserialization, the InputObjectStream class’s readObject() method is used.
1. The writeObject() method has the following syntax:
IO Exception is thrown by public final void writeObject(Object o).
2. The readObject() method has the following syntax:
final public presentation IOException, ClassNotFoundException is thrown by readObject().
To understand how to serialize an object in Java, consider the following example.
The Java platform offers a default method for the serialization of serializable objects. A (Java) class is a collection of instructions that describes how to do something.
Serialization has a lot of benefits. Some of its primary usefulness is as follows:
There are a few prerequisites for serializing an object. These are the conditions and considerations to assume when utilizing Java serialization.
Hence serialization in Java means transforming a Java object into a stream of bytes. When the end-user receives this stream of bytes, it is transformed into an object, which can be easily transported from one JVM to another. It’s worth noting that the byte stream is platform-agnostic. You can change a stream of bytes into an object and run it in any context once you have it.
1603, Capitol Avenue, Suite 413A, 2659, Cheyenne, WY 82001, USA