Difference between readObject and readResolve method in serialization



Serialization is the process of converting object to stream byte and storing byte stream in database or memory. Class which implements java.io.Serializable interface can be serialized. 

Class which is implementing this interface gives the responsibility to JVM for serialize or persist java object 

As per the oracle docs −

readObject method − Each subclass of a serializable object may define its own readObject method. If a class does not implement the method, the default serialization provided by defaultReadObject will be used. When implemented, the class is only responsible for restoring its own fields, not those of its supertypes or subtypes.The readObject method of the class, if implemented, is responsible for restoring the state of the class. The values of every field of the object whether transient or not, static or not are set to the default value for the fields type.

The readResolve method is called when ObjectInputStream has read an object from the stream and is preparing to return it to the caller. ObjectInputStream checks whether the class of the object defines the readResolve method. If the method is defined, the readResolve method is called to allow the object in the stream to designate the object to be returned. The object returned should be of a type that is compatible with all uses. If it is not compatible, a ClassCastException will be thrown when the type mismatch is discovered.

Sr. No.
Key
ReadObject()
ReadResolve()

1

Basic 

The readObject method is used to deserialize an object from the stream

The readResolve method is called when objectInputStream has read an object from the stream and is preparing to return it to the caller

2

Class 

It is part of ObjectInputStream class 

It is part of ObjectInputStream 

3


It does not check the construction of object 

The readResolve method is not invoked on the object until the object is fully constructed

Example ReadObject

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class Main {
   public static void main(String[] args) {
      User object = new User(1, "Ram");
      String filename = "tutorialPoints.ser";
      User object1 = null;
      try {
         // Reading the object from a file
         FileInputStream file = new FileInputStream(filename);
         ObjectInputStream in = new ObjectInputStream(file);
         object1 = (User) in.readObject();
         in.close();
         file.close();
      }
      catch (IOException ex) {
         ex.printStackTrace();
      }
      catch (ClassNotFoundException ex) {
         ex.printStackTrace();
      }
   }
}
class User implements java.io.Serializable {
   public int id;
   public String name;
   // Default constructor
   public User(int id, String name) {
      this.id = id;
      this.name = name;
   }
}
raja
Published on 21-Jan-2020 12:13:16
Advertisements