Welcome to the "Serialization and Deserialization in Java" repository! This repository contains three projects that demonstrate simple implementations of serialization and deserialization in Java. Each project showcases a different example and can be used as a reference to understand these concepts better.
Serialization in Java is the concept of representing an objectβs state as a byte stream. The byte stream has all the information about the object. Usually used in Hibernate, JMS, JPA, and EJB, serialization in Java helps transport the code from one JVM to another and then de-serialize it there.
Deserialization is the exact opposite process of serialization where the byte data type stream is converted back to an object in the memory. The best part about these mechanisms is that both are JVM-independent, meaning you serialize on one JVM and de-serialize on another.
Serialization offers a plethora of benefits. Some of its primary advantages are:
β¨ Used for marshaling (traveling the state of an object on the network)
β¨ To persist or save an objectβs state
β¨ JVM independent
β¨ Easy to understand and customize
To serialize an object, there are a few conditions to be met. Some other key points need to be highlighted before you proceed further in the article. These are the conditions and points to remember while using serialization in Java.
β Serialization is a marker interface with no method or data member
β You can serialize an object only by implementing the serializable interface
β All the fields of a class must be serializable; otherwise, use the transient keyword (more about it later)
β The child class doesnβt have to implement the Serializable interface, if the parent class does
β The serialization process only saves non-static data members, but not static or transient data members
β By default, the String and all wrapper classes implement the Serializable interface
Since you now know what serialization in Java is, and all the relevant points, letβs delve deep into how to serialize an object. You must use the writeObject() method of the ObjectOutputStream class for serialization and readObject() method of the InputObjectStream class for deserialization purpose.
Syntax for the writeObject() method:
public final void writeObject(Object o) throws IO Exception
Syntax for the readObject() method:
public final Object readObject() throws IOException, ClassNotFoundException
Letβs look at an example to understand how to serialize and de-serialize an object in Java.
The following program code will serialize a student object and save it to a file named student.ser.
===================================================
In the code below, you will look at how to deserialize the student object that was have serialized in the above example.
This project demonstrates a basic implementation of serialization and deserialization in Java. It covers the essential concepts and provides a starting point for understanding how to serialize and deserialize objects effectively.
In this project, we explore a more advanced implementation of serialization and deserialization. It delves into complex object structures, inheritance, and how to handle edge cases when serializing and deserializing objects.
The third project in this repository showcases serialization and deserialization with external libraries or frameworks. It demonstrates how to leverage popular libraries to simplify the serialization and deserialization process and increase productivity.
To get started with any of the examples, follow the steps below:
- Clone this repository:
git clone https://github.com/Shubh2-0/Serialization-in-Java.git
- Navigate to the specific example you want to explore:
cd ExampleX
(replaceX
with the example number) - Build and run the project using your preferred Java IDE or build tool.
Here are some resources that can help you dive deeper into serialization and deserialization in Java:
- Oracle Documentation on Serialization
- Java Object Serialization Specification
- Baeldung: Java Serialization
Contributions to this repository are welcome! If you have any suggestions, bug fixes, or additional examples, please feel free to open an issue or submit a pull request.
If you want to contact me, you can reach me through below handles.