Why does one object written to two different streams represent two different objects when reading?

The name speaks of my need, and here is the following code that I use:

Test case

SameObjectDifferentStreams same = new SameObjectDifferentStreams(); ObjectOutputStream out1 = new ObjectOutputStream(new FileOutputStream("file1")); ObjectOutputStream out2 = new ObjectOutputStream(new FileOutputStream("file2")); out1.writeObject(same); out1.close(); out2.writeObject(same); out2.close(); System.out.println("The Original reference is :" + same.toString()); oin1 = new ObjectInputStream(new FileInputStream("file1")); oin2 = new ObjectInputStream(new FileInputStream("file2")); SameObjectDifferentStreams same1 = (SameObjectDifferentStreams) oin1.readObject(); System.out.println("The First Instance is :" + same1.toString()); SameObjectDifferentStreams same2 = (SameObjectDifferentStreams) oin2.readObject(); System.out.println("The Second Instance is :" + same2.toString()); 

Output

 The Original reference is : serialization.SameObjectDifferentStreams@9304b1 The First Instance is : serialization.SameObjectDifferentStreams@190d11 The Second Instance is : serialization.SameObjectDifferentStreams@a90653 
+4
source share
4 answers

When you write an object to a stream, you actually serialize it, i.e. record only your data. Then, when you read it, you read the data and create a new object. This is similar to calling new MyObject() with the appropriate constructor arguments. Obviously, a new object is being created here.

If you read the same serialized object twice, you create a new instance twice. These two instances are equal, i.e. All their fields are equal, but the links are different, so the expression o1==o2 returns false while (if you use the reasonable equals() method) o1.equals(o2) returns true.

+6
source

This problem is called EQ in distributed computing, and some systems try to solve it, but Java deserialization does not.

Imagine two different threads residing in two different Java virtual machines in two different processes. Two different readings should lead to two different objects, since there is no way to implement a stream implementation so that these bytes in this file correspond to this object already in memory, but in a different process.

The same problem exists when two threads are in the same virtual machine - mapping from byte to object in memory is not defined, because Java serialization does not assign a global unique address to the object, and even if that were the case, it would be impossible to provide uniqueness even within one watt without a global weak map of objects containing a key record for each serialized / deserialized object. Java serialization simply searches for the security of an object identifier between different deserialization sessions.

+4
source

If they were not two different objects, the use of objects would be impossible. Consider the analogy in the physical world: I write a letter and send it to you. You start to read. Then I start changing the letter to send it to someone else, and your copy of the letter starts to change!

+4
source

They are different because when you write an object, you write a copy of this object. When you read it, this copy is placed inside a new object allocated in memory. If you compare the same1 and the same2, they will be different objects in memory.

I suspect you are comparing somewhere, for example

 same1 == same2 

and returns false. These are different objects in memory. Try overriding the equals method and compare some fields for equality.

+2
source

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


All Articles