Introduction
A key concept in Java is serialization, which entails turning objects into byte streams so that they can be transferred between Java Virtual Machines and recreated in their original state.
What is Serialization in Java?
In Java, the idea of describing an object's state as a stream of bytes is known as serialization. All of the information about the item is contained in the byte stream. The byte stream contains all the information about the object. Commonly used in Hibernate, JMS, JPA, and EJB, Java Serialization helps to transfer code from one JVM to another and de-serialize it there.
Deserialization is the exact opposite of serialization, where a stream of byte data is converted back to an object in memory. The best thing about these mechanisms is that they are both JVM independent, meaning you serialize on one JVM and de-serialize on another.
Why Do We Need Serialization in Java?
We need serialization for the following reasons:-
Communication: Serialization involves the procedure of serializing and transferring an object. This allows multiple computer systems to simultaneously design, share, and execute objects.
Caching: The time taken to create an object is longer than the time taken to de-serialize the object. Serialization minimizes overhead by caching oversized objects.
Deep Copy: Serialization simplifies the cloning procedure. By serializing an object into a byte array and then deserializing it, one can create an exact duplicate of the object.
Cross JVM Synchronization: The fundamental benefit of serialization is that it is compatible with various JVMs that may operate on various operating systems or architectures.
Persistence: The state of any object can be directly saved by serializing it and stored in the database for later retrieval.
Our Learners Also Read: All You Should Know About Substrings in Java
Advantages and Disadvantages of Serialization in Java
Advantages:
- The serialization process is a built-in feature that does not require third-party software to run serialization.
- The serialization process turned out to be simple and easy to understand.
- The serialization procedure is universal and familiar to developers from various environments.
- Easy to use and easy to customize.
- Encryption, compression, authentication, and safe Java computing are all supported via serialized data streams.
- Many critical technologies rely on serialization.
Disadvantages:
- Because serialization is a built-in functionality, it can be used without any additional software.
- The serialization procedure proved to be straightforward and simple to comprehend.
- The serialization process is standard and well-known to developers working in a variety of settings.
- both simple to use and to customize.
- Serialized data streams, encryption, compression, authentication, and secure Java computing are all supported.
- Serialization is a key component of many crucial technologies.
Points to Note About Serialization in Java?
Several conditions must be met to serialize an object. Before proceeding further in the article, there are some other key points to highlight. These are the prerequisites and things to keep in mind when utilizing Java serialization.
- Serialization is a tag interface with no method or data member.
- You can only serialize an object by implementing a serializable interface.
- All fields of a class must be serializable; otherwise, use the transitive keyword (more on that later)
- A child class need not implement the Serializable interface if the parent class does
- The serialization process stores only non-static data members, not static or transient ones.
- By default, String and all wrapper classes implement the Serializable interface.
Practical Examples of Serialization in Java
Serialization using inheritance
If the superclass is serializable, then its subclasses are also serializable by default.
In this case, the subclass is serializable by default if the superclass implements the Serializable Interface.
```
package SerializationInheritance;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class A implements Serializable{
int i;
public A(int i){
this.i = i;
}
}
class B extends A{
int j;
public B(int i, int j){
super(i);
this.j = j;
}
}
public class Test{
public static void main(String[] args)throws Exception{
B b1 = new B(200,400);
System.out.println("i = " + b1.i);
System.out.println("j = " + b1.j);
FileOutputStream fos = new FileOutputStream("abc.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(b1);
oos.close();
fos.close();
System.out.println("The object has been serialized");
FileInputStream fis = new FileInputStream("abc.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
B b2 = (B)ois.readObject();
ois.close();
fis.close();
System.out.println("The object has been deserialized");
System.out.println("i = " + b2.i);
System.out.println("j = " + b2.j);
}
}
```
Output:
```
j = 20
The object has been serialized
The object has been deserialized
i = 200
j = 400
```
Conclusion
This article taught us about serialization in Java, its usage, and its implementation. Serialization is essential in everyday Programming, especially when transferring objects over a network.