What values will int and Integer will be initialized to during DeSerialization process if they were not part of Serialization in java




int will be initialized to 0 and Integer will be initialized to null during DeSerialization (if they were not part of Serialization process).





Full Program/SourceCode to show that int is initialized to 0 and Integer is initialized to null during DeSerialization (if they were not part of Serialization process) >


package SerDeser8intIntegerInitialized;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */

/*Author : AnkitMittal  Copyright- contents must not be reproduced in any form*/
class Employee implements Serializable  {
  
   private static final long serialVersionUID = 1L;
   private int idInt;
   private Integer idInteger;
   private String name;
  
   public Employee(String name) {
          this.name = name;
   }
   @Override
   public String toString() {
          return "Employee [idInt=" + idInt + ", idInteger=" + idInteger
                       + ", name=" + name + "]";
   }
   private void writeObject(ObjectOutputStream os) throws IOException {
          System.out.println("In, writeObject() method.");   
          os.writeObject(this.name);
         
   }
  
   /*
   * define how deSerialization process will read objects.  
   */
   private void readObject(ObjectInputStream ois) {
          System.out.println("In, readObject() method.");
          try {
                 name=(String)ois.readObject();
          } catch (Exception e) {
                 e.printStackTrace();
          }
   }
}
public class IntIntegerValueDuringDeSerialization {
   public static void main(String[] args) {
          Employee object1 = new Employee("ankit");
          try {
                 OutputStream fout = new FileOutputStream("ser.txt");
                 ObjectOutput oout = new ObjectOutputStream(fout);
                 System.out.println("Serialization process has started, serializing employee objects...");
                 oout.writeObject(object1);
                 fout.close();
        oout.close();
                 System.out.println("Object Serialization completed.");
                
                 //DeSerialization process >
                
                
                 InputStream fin=new FileInputStream("ser.txt");
                 ObjectInput oin=new ObjectInputStream(fin);
                 System.out.println("\nDeSerialization process has started, displaying employee objects...");
                 Employee emp=(Employee)oin.readObject();
                 System.out.println(emp);
                 fin.close();
       oin.close();
                 System.out.println("Object DeSerialization completed.");
                
                
          } catch (IOException | ClassNotFoundException  e) {
                 e.printStackTrace();
          }
   }
}
/*OUTPUT
Serialization process has started, serializing employee objects...
Object Serialization completed.
DeSerialization process has started, displaying employee objects...
Employee [idInt=0, idInteger=null, name=ankit]
Object DeSerialization completed.
*/
If we note output, int was be initialized to 0 and Integer was initialized to null.



RELATED LINKS>

Serializing & DeSerializing >

More about Serialization >






Interviews >


No comments:

Post a Comment