Serialization in Java

Back to home
Logicmojo - Updated Jan 11, 2023



Introduction

Serialization is the act of transforming an object from a java supported form to either a network supported form (or) a file supported form by storing (or) writing its state to a file.

We may perform serialisation by using the FileOutputStream and ObjectOutputStream classes.

Only Serilizable objects are capable of serialisation.

  1. If and only if the matching class implements the Serializable interface, an object is considered to be Serilizable.

  2. There are no methods in the Serializable interface, which is part of the java.io package. It's a marker user interface. JVM will automatically offer the appropriate capability..

  3. We can add any number of objects to the file, and we can read all of those objects from the file, but only the objects written in the same sequence will be returned. The value of organisation cannot be emphasised.


Why do we need Serialization in Java?

We need Serialization for the following reasons:

🚀 Communication :Serialization is the process of serialising and transmitting objects. Multiple computer systems can now develop, share, and execute items at the same time.

🚀 Caching :When compared to the time required to de-serialize an object, the time spent creating it is longer. By caching the big objects, serialisation reduces time consumption.

🚀 Deep Copy :The use of Serialization simplifies the cloning procedure. By serialising an object to a byte array and then de-serializing it, an exact clone of the item can be retrieved.

🚀 Cross JVM Synchronization : Serialization has the primary benefit of working across different JVMs that may be running on different architectures or operating systems.

🚀 Persistence : Any object's state can be immediately saved by applying Serialization to it and storing it in a database to be retrieved later.

How do we Serialize an Object?

If and only if its class or any of its parent classes implement the java.io.Serializable interface or its subinterface, java.io.Externalizable, a Java object is serializable.

We turn an object's state into a byte stream so that it can be transported from one JVM to another, and then we restore the byte stream back to the original object through the Serialization process.

package Serial1;
 
import java.io.Serializable;
public class Employee implements Serializable{
       private static final long serialVersionUID = 1L; //Serial Version UID
       int id;
       String name;
       public Employee(int id, String name) {
             this.id = id;
             this.name = name; 
       }
}

package Serial1;
 
import java.io.*;
class Persist{
       public static void main(String args[]){
               try{
                      Employee emp1 =new Employee(20110,"John");
                      Employee emp2 =new Employee(22110,"Jerry");
                      Employee emp3 =new Employee(20120,"Sam");
                      FileOutputStream fout=new FileOutputStream("output.txt");
                      ObjectOutputStream out=new ObjectOutputStream(fout);
                      out.writeObject(emp1);
                      out.writeObject(emp2);
                      out.writeObject(emp3);
                      out.flush();
                      out.close();
                      System.out.println("Serialization and Deserialization is been successfully executed");
               }
               catch(Exception e){
                      System.out.println(e);}
               }
}


Advantages and Disadvantages

Advantages

  1. Serialization is a built-in capability that does not necessitate the use of any third-party software.

  2. The serialisation technique has been shown to be simple and straightforward.

  3. Serialization is a universal method that is recognisable to developers of all backgrounds.

  4. It is easy to use and simple to customize

  5. Encryption, compression, authentication, and safe Java computing are all supported via serialised data streams.

  6. Serialization is used in a variety of important technologies.

Disadvantages

  1. Objects become brittle during DeSerialization and are not guaranteed to be DeSerialized effectively.

  2. The declaration of transient variables during Serialization produces memory space, but the constructor is not called, resulting in the failure of transient variable initialization, resulting in a deviation from the Standard Java Flow.

  3. In terms of memory usage, the serialisation procedure is inefficient.

  4. Serialization is not recommended for usage in applications that require concurrent access without the use of third-party APIs since it lacks a transition control mechanism for each SE.

  5. The serialisation technique does not provide fine-grained access control to Objects.




With this article at Logicmojo, you must have the complete idea of Serialization in Java