Serialisation and Deserialization in Java

Serialization in Java:

Serialization is a mechanism of writing the state of an object into a byte stream. It is mainly used to travel an object’s state on the network.

The string class and all the wrapper classes implement a Serializable interface by default. It is a marker interface. It is just used to “mark” Java classes that support a certain capability. It must be implemented by the class whose object we want to persist.

Example of Serialization: In this example, we are going to serialize the object of the Employee class. The writeObject() method of ObjectOutputStream class provides the functionality to serialize the object. We’re saving the state of the object in the file named f.txt.

class Persist
  public static void main(String args[]) throws Exception{
  Employee e1=new Employee(111, "Anjan");
  FileOutputStream fos=new FileOutputStream("f.txt");
  ObjectOutputStream oos=new ObjectOutputStream(fos);

Deserialization in Java:

The reverse operation of the serialization is called Deserialization.