For, example if the object instance was serialized (resulting in 'byte[] bs1') then serialized again (resulting in 'byte[] bs2'), should 'bs1' and 'bs2' be the same length and contain the same bytes? If not, why not?
To avoid ambiguity, I should say that the two serializations of the same object (not two 'identical' objects - the same instance), happened within milliseconds of each other, in the same thread, on the same JVM - one before the (mutable) object was passed to a method, one after.
Note, that the object is not even being serialized, deserialized, and then re-serialized - it is just being serialized twice. Also, there is nothing at all 'clever' about the object's class; it is just a simple, aggregate POJO.
I'm using the serialized bytes as an ad hoc test to detect if the object has been changed inside the method. However, I seem to be seeing examples where the object has clearly not changed its state - yet one byte[] is different from the other.
I was assuming - and it was just an assumption - that they would be the same. Is there some reason they might not be?
Addendum:
Also, apologies for no example code to illustrate the point. Currently this is a small piece of code embedded in a large system. I'll try and extract a smaller, runnable demonstration, if necessary. I wondered if there was a basic issue with my assumption though, and therefore if anyone could explain why the assumption is wrong.
To serialize an object means to convert its state to a byte stream so way that the byte stream can be reverted back into a copy of the object. A Java object is serializable if its class or any of its superclasses implements either the java.
To make a Java object serializable we implement the java. io. Serializable interface. The ObjectOutputStream class contains writeObject() method for serializing an Object.
What happens if you try to send non-serialized Object over network? When traversing a graph, an object may be encountered that does not support the Serializable interface. In this case the NotSerializableException will be thrown and will identify the class of the non-serializable object.
A Java object is serializable if and only if its class or any of its parent classes implement either the java. io. Serializable interface or its subinterface, java.
It might not always be the exact same sequence of bytes that is produced through serialization. For example, the order in which a Set
serializes its elements is not guaranteed to be constant, even if we're talking about a single instance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With