What happens during serialization in java, if two object refrences are pointing to the same serializable Object? -


what happens during serialization in java, if 2 object refrences pointing same serializable object? does serializable objects saved twice ?
example :

    class king implements java.io.serializable {         private string name="akbar";     }      class kingdom implements java.io.serializable {         king goodking=new king();         king badking=goodking;     }      public class testserialization {         public static void serializeobject(string outputfilename,                                            object serializableobject) throws ioexception {             fileoutputstream filestream=new fileoutputstream(outputfilename);             objectoutputstream outstream=new objectoutputstream(filestream);             outstream.writeobject(serializableobject);             outstream.close();         }          public static void main(string[] args) {             kingdom kingdom=new kingdom();             try {                 testserialization.serializeobject("kingdom1.out", kingdom);             }catch(ioexception ex) {                 ex.getmessage();             }         }     } 

now, whether 1 object state saved both goodking , badking refrences or king object saved twice ?

the documentation objectoutputstream says happens:

the default serialization mechanism object writes class of object, class signature, , values of non-transient , non-static fields. references other objects (except in transient or static fields) cause objects written also. multiple references single object encoded using reference sharing mechanism graphs of objects can restored same shape when original written.

(my emphasis)

e.g., if have multiple references single object, when graph reconstituted, end multiple references single reconstituted version of object, not references multiple equivalent instances of it.

of course, if container being serialized implements different mechanism, behavior dictated mechanism, not default one.

so instance, if have thing , test:

thing.java:

import java.io.*; import java.util.*;  public class thing implements serializable {     private map<string,string> map1;     private map<string,string> map2;      public thing() {         this.map1 = new hashmap();         this.map2 = this.map1; // referring same object     }      public void put(string key, string value) {         this.map1.put(key, value);     }      public boolean mapsaresameobject() {         return this.map1 == this.map2;     } } 

test.java:

import java.io.*;  public class test implements serializable {      public static final void main(string[] args) {         try         {             // create thing             thing t = new thing();             t.put("foo", "bar");              // write out             objectoutputstream os = new objectoutputstream(new fileoutputstream("foo"));             os.writeobject(t);             os.close();             os = null;              // read in             thing t2;             objectinputstream = new objectinputstream(new fileinputstream("foo"));             t2 = (thing)is.readobject();             is.close();             = null;              // same underlying map both properties?             system.out.println("t2.mapsaresameobject? " + t2.mapsaresameobject());         }         catch (exception e)         {             system.out.println("exception: " + e.getmessage());         }     } } 

and run java test, get:

t2.mapsaresameobject? true

...because both of thing's members, map1 , map2 end pointing single hashmap instance.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -