views:

299

answers:

5

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?

A: 

The most obvious is that you can transmit the serialized class over a network, and the recepient can construct a duplicate of the original instanstance. Likewise, you can save a serialized structure to a file system.

Also, note that serialization is recursive, so you can serialize an entire heterogenous data structure in one swell foop, if desired.

ddyer
+1  A: 

In essense:

Serialization is the process of converting a set of object instances that contain references to each other into a linear stream of bytes, which can then be sent through a socket, stored to a file, or simply manipulated as a stream of data

See uses from Wiki:

Serialization has a number of advantages. It provides:

  1. a method of persisting objects which is more convenient than writing their properties to a text file on disk, and re-assembling them by reading this back in.
  2. a method of issuing remote procedure calls, e.g., as in SOAP
  3. a method for distributing objects, especially in software componentry such as COM, CORBA, etc.
  4. a method for detecting changes in time-varying data.

Also see here for loads more info...

Pool
A: 

Serialized objects maintain state in space, they can be transferred over the network, file system, etc... and time, they can outlive the JVM that created them.

Sometimes this is useful.

David Soroko
This can be achieved with a simple file containing some text also. It is a little easier in reading back a serialized object then reading the state of the object written to a text file, correct?
m_a_khan
A: 

Serialization allows your objects to be saved and opened just like you might text files. When creating and using classes, they all disappear when the application quits. With serialization, you can save them, transmit them around, and open them wherever you like.

Without serialization, we would have to start from a complete blank slate each time our program ran. Everything would disappear when you closed the application, never to be found again. (sounds pretty useless, no? Very few programs don't use serialization/file-IO in some way shape or form.)

CrazyJugglerDrummer
So, it seems like it is just a better, more efficient way of writing data to a file and reading it back when needed?
m_a_khan
+6  A: 

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.

Schmelter
What on earth takes 10 minutes to build?
oxbow_lakes
My point being (of course) that the file I/O involved in serialization will likely dwarf any pure object creation overhead. I suppose you might be talking about something computationally very expensive like scientific modelling but serialization is a very poor mechanism for persistence due to it being difficult to handle schema changes
oxbow_lakes
@oxbow_lakes An example might be if you maintain an index of a particular set of data for fast searching. An index like that can take a very long time to build, but once you have it built it can be serialised/de-serialised relatively quickly.
David
Touché! Fair point
oxbow_lakes