Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should a serialized Java object always be represented by the same sequence of bytes each time is it serialized?

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.

like image 987
Paul Avatar asked Jun 21 '13 17:06

Paul


People also ask

What happens if the object to be serialized?

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.

What method should we use for writing object in serialization in 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 object is not serialized in Java?

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.

Can we serialize every object in Java?

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.


1 Answers

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.

like image 122
arshajii Avatar answered Oct 30 '22 21:10

arshajii