What Is Object Serialization

What is object serialization?

Serialization is the conversion of an object to a series of bytes, so that the object can be easily saved to persistent storage or streamed across a communication link. The byte stream can then be deserialized - converted into a replica of the original object.

What is a serialized object in programming?

Serialization usually refers to the process of converting an abstract datatype to a stream of bytes (You sometimes serialize to text, XML or CSV or other formats as well. The important thing is that it is a simple format that can be read/written without understanding the abstract objects that the data represents). When saving data to a file, or transmitting over a network, you can't just store a MyClass object, you're only able to store bytes. So you need to take all the data necessary to reconstruct your object, and turn that into a sequence of bytes that can be written to the destination device, and at some later point read back and deserialized, reconstructing your object.

Why do we use serialization?

Technically on the low-level, your serialized object will also end up as a stream of bytes on your cable or your filesystem...

So you can also think of it as a standardized and already available way of converting your objects to a stream of bytes. Storing/transferring object is a very common requirement, and it has less or little meaning to reinvent this wheel in every application.

As other have mentioned, you also know that this object->stream_of_bytes implementation is quite robust, tested, and generally architecture-independent.

This does not mean it is the only acceptable way to save or transfer an object: in some cases, you'll have to implement your own methods, for example to avoid saving unnecessary/private members (for example for security or performance reasons). But if you are in a simple case, you can make your life easier by using the serialization/deserialization of your framework, language or VM instead of having to implement it by yourself.

Hope this helps.

What does Serializable mean?

Serialization is persisting an object from memory to a sequence of bits, for instance for saving onto the disk. Deserialization is the opposite - reading data from the disk to hydrate/create an object.

In the context of your question, it is an interface that if implemented in a class, this class can automatically be serialized and deserialized by different serializers.

What is serialization in Java?

Serializable is a marker interfaces that tells the JVM it can write out the state of the object to some stream (basically read all the members, and write out their state to a stream, or to disk or something). The default mechanism is a binary format. You can also use it to clone things, or keep state between invocations, send objects across the network etc.

You can let eclipse generate one for you (basically just a long random but unique ID). That means you can control when you think a class would be compatible with a serialized version, or not.

(Note: that all the non transient member variables must be of a serializable class, or you will get an error - as the JVM will recurse through the structure writing out the state of each object down to the level of writing primitives to the ObjectOutputStream).



Related Topics



Leave a reply



Submit