Is a deserialised object the same instance as the original

前端 未结 5 1891
心在旅途
心在旅途 2021-02-04 02:07

When I instantiate an object from a class, an object is saved in the java heap. When I save the object by serializing it and I later deserialize the object, do I understand corr

5条回答
  •  孤街浪徒
    2021-02-04 02:34

    No they will not be the same object in memory originalObj == deserilized will be false, however originalObj.equals(deserilized) should be true.

    Objects B, C, D and E. All of them when instantiated, had Object A. Then, let's assume I serialize and deserialized all of them. When I change a field of Object A after deserialization, is there a way to reflect such change in the Object A(s) in BCDE?

    If I understand you correctly the answer is no, the references will not be pointing to the same Object A

    However if you so desire you can explicitly set all of the references of Object A in each object B, C, D and E to point to the same instance of Object A

    Here is a demo to illustrate the points made.

    import java.io.*;
    import java.util.*;
    
    public class Demo {  
      public static void main(String... aArguments) {  
        List quarks = Arrays.asList(
          new Quark("up"), new Quark("down")
        );
    
        serialize(quarks);
        List recoveredQuarks = deserialize();
    
        System.out.println(quarks == recoveredQuarks);               // false
        System.out.println(quarks.equals(recoveredQuarks));          // true
    
        System.out.println(quarks.get(0) == recoveredQuarks.get(0)); // false
    
        // but you can set it to the same instance
        recoveredQuarks.set(0, quarks.get(0));
        System.out.println(quarks.get(0) == recoveredQuarks.get(0)); // true
    
        quarks.get(0).name = "Charm";
        boolean b = quarks.get(0).name == recoveredQuarks.get(0).name;
        System.out.println(b);                                       // true
      }
    
      static void serialize(List quarks) {
        try {
          OutputStream file = new FileOutputStream("quarks.ser");
          OutputStream buffer = new BufferedOutputStream(file);
          ObjectOutput output = new ObjectOutputStream(buffer);
          output.writeObject(quarks);
          output.close();
        }
        catch(IOException ex) { ex.printStackTrace(); }
      }
    
      static List deserialize() {
        List recoveredQuarks = null;
        try {
          InputStream file = new FileInputStream("quarks.ser");
          InputStream buffer = new BufferedInputStream(file);
          ObjectInput input = new ObjectInputStream(buffer);
          recoveredQuarks = (List)input.readObject();
          input.close();
        } 
        catch(ClassNotFoundException ex){ }
        catch(IOException ex){ ex.printStackTrace(); }
        return recoveredQuarks;
      }
    }
    
    class Quark implements Serializable {
        String name;
        Quark(String name) {
          this.name = name;
        }
    
        @Override
        public boolean equals(Object o) {
          if (o != null && o instanceof Quark) {
            return this.name.equals(((Quark)o).name);
          }
          return false;
        }
    }
    

提交回复
热议问题