Topic outline

    • Java provides a mechanism, called object serialization where an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object.


      After a serialized object has been written into a file, it can be read from the file and deserialized that is, the type information and bytes that represent the object and its data can be used to recreate the object in memory.


      Most noteworthy is that the whole mechanism is independent of JVM, which ensures that an object can be serialised and deserialized on an entirely different platform on one platform. 


      The ObjectInputStream and ObjectOutputStream classes are high-level streams containing serialisation and deserialization methods for an object.


      Most noteworthy is that the whole mechanism is independent of JVM, which ensures that an object can be serialised and deserialized on an entirely different platform on one platform. 


      The ObjectInputStream and ObjectOutputStream classes are high-level streams containing serialisation and deserialization methods for an object.


      Example- 


      package examples;

      import java.io.*;

      class serailTest implements java.io.Serializable

      {

      public int test_number; 

      public String test_string; 

      public serailTest(int test_number, String test_string) 

      this.test_number = test_number;

      this.test_string = test_string;

      }


      public class serialization_example {

      public static void main(String[] args) 

      {

      serailTest serailization_object = new serailTest(26, "Great"); 

      FileOutputStream file_out;

      ObjectOutputStream out;

      //Serialization

      try 

      {

      file_out = new FileOutputStream("Test_file.ser");

      out = new ObjectOutputStream(file_out); 

      out.writeObject(serailization_object); 

      out.close(); 

      file_out.close(); 


      System.out.println("Serialized"); 

      catch (IOException e) 

      {

      System.out.println("IOException is caught");

      Note that two conditions must be fulfilled for a class to be effectively serialised- 


      1. The Java.io. Serializable Interface must be implemented by the class. 
      2. Both fields in the class must be capable of serialisation. When a field can not be serialised, it must be identified as transient.


      An Object Deserializing 


      The Employee object generated in the SerializeDemo programme deserializes the following DeserializeDemo programme. Studying the software and attempting to evaluate its performance.


      //Deserialization 

      ObjectInputStream in;

      FileInputStream file_in;

      serailTest deserailization_object = null; 

      try 

      {

      file_in = new FileInputStream("Test_file.ser");

      in = new ObjectInputStream(file_in); 

      deserailization_object = (serailTest)in.readObject(); 

      in.close(); 

      file_in.close(); 


      System.out.println("Deserialized"); 

                  System.out.println("Test Number = " + deserailization_object.test_number); 

                  System.out.println("Test String = " + deserailization_object.test_string); 

      catch (IOException | ClassNotFoundException e) 

      {

      System.out.println("IOException is caught");

      }


      }


      The following relevant points to be noted here are: 


      1. The try / catch block attempts to catch a ClassNotFoundException, which the readObject) (method declares. In order for a JVM to be able to deserialize an entity, the bytecode for the class must be found. If, during deserialization of an entity, the JVM can not locate a class, it throws a ClassNotFoundException.
      2. Note that the readObject) (return value is converted to an Employee reference. 
      3. When the object was serialised, the value of the SSN field was 11122333, but since the field is transient, this value was not sent to the output stream. The Deserialized Employee Object SSN field is 0.0.