Master java skills

Serialization in Java

In Java, the term “Serialization” refers to the idea of encoding an object’s state as a byte-stream. The object’s whole information is contained in the byte-stream. The concept of serialization is frequently utilized in areas like Hibernate, JMS, JPA, and EJB.
Serialization concept helps transport the code from one JVM to another and then de-serialize it there.
The reverse operation of serialization is called deserialization where object in the memory is formed from its byte-stream. Both the operations of serialization and deserialization are platform-independent, it means you can serialize an object on one platform and deserialize it on a different platform.

Need For Serialization

The following justifies our necessity for serialization:

  • Communication: The processes of object serialization and transmission are involved in serialization. This makes it possible for several computers to design, share, and use objects at once.
  • Deep CopyBy employing Serialization, cloning is simplified. By serializing an object to a byte array and then de-serializing it, one can create an exact copy of the object.
  • Caching: When compared to the time needed to de-serialize an object, creating one takes longer. By caching the massive objects, serialization reduces time consumption.
  • Persistence: By using serialization on it, every object’s state can be directly saved and afterwards retrieved by storing it in a database.

Important Points to be noted for Serialization

A few requirements must be satisfied in order to serialize an object. Before reading the rest of the article, there are a few additional important aspects that should be emphasized. These are the prerequisites and things to keep in mind when utilizing Java serialization.

  • Only by implementing the serializable interface can you serialize an object.
  • Serialization is a marker interface with no data member or method.
  • If a class doesn’t have all of its fields serializable, use the transient keyword.
  • If the parent class implements the Serializable interface, the child class is not required to do so.
  • Only non-static data members are saved during the serialization process; neither static nor transitory data members are saved.
  • All wrapper classes and the String by default implement the Serializable interface.