What Is Serialization In Java?

In Java, serialization turns an object's state to a byte stream, whereas deserialization does the opposite. Serialization is the process of transforming a Java object into a static stream (sequence) of bytes that may be stored in a database or transmitted over the internet. Deserialization uses the byte stream to recreate the actual Java object in memory. This method is used to maintain the life of an object.

broken image

Let's look at a real-world example before we go into the specifics of serialization.

Assume you're talking to a friend on a cellular network. Your voice can be heard hundreds of miles away by a buddy. You may also be aware that your voice is converted into electrical signals and then transmitted to a mobile tower via radio signals. The electrical signal is then channelled by the tower to the specific phone number with which you are connected.

Before it reaches your companion on the other side, your voice gets converted numerous times. In the same way, Java objects also require serialization and deserialization when transferring from one Java Virtual Machine to another.

broken image

The JVM provides additional features to the class that implements the Serializable Interface.

A method called "writeObject" exists in the class "ObjectOutputStream." This method is responsible for converting an object into a Byte stream. readObject() is a method in the class "ObjectInputStream" (). A byte stream is converted to a string using this method.

Syntex

broken image

Java.io. Serializable Interface

Data can be serialized using the Serializable interface (has no data member and method). It is used to "mark" Java classes in order for their objects to have specified capabilities.

● The Serializable interface must be implemented by the class whose object has to be persisted.

● The String class and all wrapper classes implement the java.io package.

● The interface is serializable by default.

Java's ObjectOutputStream class

This class is necessary for writing objects into streams. This class's methods can only take classes that implement the Serializable interface as arguments. Using the methods in this class, the object is transformed to a byte stream.

 

Constructor in Java

Let's have a look at constructor function of this class-

broken image

The Object() function generates a byte stream that writes to an Object stream.

 

Methods in Java

 

Let's have a look at themethods contained within this class. There are three methods in this class. 

  1. public void flush() throws IOException {} – The flushing of the output stream is the responsibility of this method. 
  2. public final void writeObject(Object obj) throws IOException {} – responsible for writing the object to the ObjectOutputStream. 
  3. public void close() throws IOException {} – This method can be used to close a current output stream. 

What are the advantages of serialization in Java?

broken image

Serialization provides numerous advantages. The following are some of its primary usefulness:

● This is a Java feature that is included by default. You won't need to rely on third-party services to implement Serialization as a result.

● Serialization is used in practically every piece of technology we use on a regular basis.

● This concept is easy to grasp and customize.

● It can be customised to the programmer's needs.

● This is a universal procedure that all developers are familiar with.

● Encryption, authentication, compression, and secure Java computing are now all possible with Java.

● It's used to marshal troops (travelling the state of an object across a network) and save or endure the state of an object that the JVM doesn't bind.

Some important considerations to make during the Serialization of an object in Java

Serializing an object requires a few conditions. When using Java serialisation, keep these criteria and factors in mind:

Some important considerations to make during the Serialization of an object in Java

Serializing an object requires a few conditions. When using Java serialisation, keep these criteria and factors in mind:

● The serialization interface has no data members or methods.

● The child class is not required if the parent class executes the Serializable interface.

● During the serialisation process, only non-static data members are preserved; static or temporary data members are not saved.

● To serialize an object, only the serializable interface can be used.

● By default, String and all wrapper classes are disabled.

● The fields of a class must all be serializable; otherwise, use the temporary keyword.

Explore more with: What is SDET