What happens during serialization in java if two refrences objects point to the same serializable object?

What happens during serialization in java if two refrences objects point to the same serializable object? Are Serializable Objects Files Saved Twice?
eg:

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, is only one state of the object saved for both goodKing and badKing refrences, or for a King object that is saved twice?

+6
source share
1 answer

The documentation for ObjectOutputStream says what happens:

The default serialization mechanism for an object records the class of the object, the class signature, and the values โ€‹โ€‹of all non-transient and non-static fields. References to other objects (with the exception of temporary or static fields) also force these objects to be written. Several links to one object are encoded using the link exchange mechanism, so that the graphics of objects can be restored in the same form as when writing the original.

(My emphasis)

For example, if you have several links to one object, when the chart is restored, you will get several links to one restored version of this object, and not links to several equivalent instances.

Of course, if the serializable container implements a different mechanism, the behavior is dictated by this mechanism, not the standard one.

So, for example, if we have Thing and 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 to 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 a Thing Thing t = new Thing(); t.put("foo", "bar"); // Write it out ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("foo")); os.writeObject(t); os.close(); os = null; // Read it in Thing t2; ObjectInputStream is = new ObjectInputStream(new FileInputStream("foo")); t2 = (Thing)is.readObject(); is.close(); is = null; // Same underlying map for both properties? System.out.println("t2.mapsAreSameObject? " + t2.mapsAreSameObject()); } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); } } } 

And run java Test , we get:

  t2.mapsAreSameObject?  true 

... because both members of Thing , map1 and map2 ultimately point to the same HashMap instance.

+7
source

Source: https://habr.com/ru/post/970612/


All Articles