Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we use serialization?

Why do we need to use serialization?
If We want to send an object or piece of data through a network We can use streams of bytes.
If We want to save some data to the disk We can again use the binary mode along with the byte streams and save it.
So Whats the advantage of using serialization?

like image 896
Hossein Avatar asked Sep 07 '12 12:09

Hossein


People also ask

Why should I use serialization?

Well, serialization allows us to convert the state of an object into a byte stream, which then can be saved into a file on the local disk or sent over the network to any other machine. And deserialization allows us to reverse the process, which means reconverting the serialized byte stream to an object again.

What is the use of serialization process?

Serialization is a mechanism of converting the state of an object into a byte stream. Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory. This mechanism is used to persist the object.

Where do we use serialization in Java?

Serialization in Java is the concept of representing an object's state as a byte stream. The byte stream has all the information about the object. Usually used in Hibernate, JMS, JPA, and EJB, serialization in Java helps transport the code from one JVM to another and then de-serialize it there.


1 Answers

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.

like image 132
niconoe Avatar answered Sep 19 '22 15:09

niconoe