Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of Serialization in Java?

I have read quite a number of articles on Serialization and how it is so nice and great but none of the arguments were convincing enough. I am wondering if someone can really tell me what is it that we can really achieve by serializing a class?

like image 900
m_a_khan Avatar asked Feb 09 '10 21:02

m_a_khan


People also ask

What is serialization and why it is used?

Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

What is the purpose of serialization in Java Mcq?

Explanation: Serialization is the process of writing the state of an object to a byte stream. This is used when you want to save the state of your program to a persistent storage area. 2.

When should we use serialization?

Here are some examples of using serialization: - Storing data in an object-oriented way to files on disk, e.g. storing a list of Student objects. - Saving program's states on disk, e.g. saving state of a game. - Sending data over the network in form objects, e.g. sending messages as objects in chat application.

Why serializable is needed?

Serialization is usually used When the need arises to send your data over network or stored in files. By data I mean objects and not text. Now the problem is your Network infrastructure and your Hard disk are hardware components that understand bits and bytes but not JAVA objects.


1 Answers

Let's define serialization first, then we can talk about why it's so useful.

Serialization is simply turning an existing object into a byte array. This byte array represents the class of the object, the version of the object, and the internal state of the object. This byte array can then be used between JVM's running the same code to transmit/read the object.

Why would we want to do this?

There are several reasons:

  • Communication: If you have two machines that are running the same code, and they need to communicate, an easy way is for one machine to build an object with information that it would like to transmit, and then serialize that object to the other machine. It's not the best method for communication, but it gets the job done.

  • Persistence: If you want to store the state of a particular operation in a database, it can be easily serialized to a byte array, and stored in the database for later retrieval.

  • Deep Copy: If you need an exact replica of an Object, and don't want to go to the trouble of writing your own specialized clone() class, simply serializing the object to a byte array, and then de-serializing it to another object achieves this goal.

  • Caching: Really just an application of the above, but sometimes an object takes 10 minutes to build, but would only take 10 seconds to de-serialize. So, rather than hold onto the giant object in memory, just cache it out to a file via serialization, and read it in later when it's needed.

  • Cross JVM Synchronization: Serialization works across different JVMs that may be running on different architectures.

like image 185
Schmelter Avatar answered Sep 21 '22 20:09

Schmelter