What is the purpose of Serialization in Java?

106,254

Solution 1

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.

Solution 2

While you're running your application, all of its objects are stored in memory (RAM). When you exit, that memory gets reclaimed by the operating system, and your program essentially 'forgets' everything that happened while it was running. Serialization remedies this by letting your application save objects to disk so it can read them back the next time it starts. If your application is going to provide any way of saving/sharing a previous state, you'll need some form of serialization.

Solution 3

I can share my story and I hope it will give some ideas why serialization is necessary. However, the answers to your question are already remarkably detail.

I had several projects that need to load and read a bunch of text files. The files contained stop words, biomedical verbs, biomedical abbreviations, words semantically connected to each other, etc. The contents of these files are simple: words!

Now for each project, I needed to read the words from each of these files and put them into different arrays; as the contents of the file never changed, it became a common, however redundant, task after the first project.

So, what I did is that I created one object to read each of these files and to populate individual arrays (instance variables of the objects). Then I serialized the objects and then for the later projects, I simply deserialized them. I didn't have to read the files and populate the arrays again and again.

Solution 4

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.

Solution 5

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.

Share:
106,254

Related videos on Youtube

m_a_khan
Author by

m_a_khan

Updated on July 08, 2022

Comments

  • m_a_khan
    m_a_khan almost 2 years

    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?

    • Yoni
      Yoni about 14 years
      Do you mean serializing classes, or objects?
    • Anthony Forloney
      Anthony Forloney about 14 years
      He "wasn't convinced" they answered his questions.
    • Anon.
      Anon. about 14 years
      The answers provided were, in fact, correct. If they don't work for you, reply to them as comments and try to work it out instead of just ignoring them and then starting to flame others when they point out you're not using SO right.
    • David
      David about 14 years
      No article should convince you to use serialisation. If you need it to solve a problem that need is what will convince you to use it. The articles just need to help you use it correctly.
    • m_a_khan
      m_a_khan about 14 years
      @Anon: Did you pay attention to the post date of my posts? I tried getting an answer for almost over a month for the same problem I was having and I was getting the same answer from the same people. I don't see how you are using SO right, I will say it one more time, mind your business, if you can't contribute anything positive then don't contribute anything.
    • Anon.
      Anon. about 14 years
      If we look at this question: stackoverflow.com/questions/1941775/… You got an answer which answered the question posted, and just ignored it.
    • Chris
      Chris about 14 years
      Anon is contributing to the quality of the site by discouraging duplicate questions. It's silly to see five separate questions that are all asking the same thing. If you can't get an answer, well sometimes that's just how it is. If you get an answer but it doesn't seem to work, then continue the discussion in comments. SO is not a magic box that will magically give you an answer if you just ask the right question, or ask enough times, or catch the eye of the right expert.
    • m_a_khan
      m_a_khan about 14 years
      @Anon: I see, so what should I do now? mark it correct/accept it? Since you somehow know that the posted answer "answered" my question, would you be kind enough to tell me what should be my next step in order to comply with SO rules?
    • Anon.
      Anon. about 14 years
      Either: (Accept the answer) or (Post a comment and/or amend the question explaining why it's not what you need).
    • Pratik Butani
      Pratik Butani almost 11 years
    • Simon
      Simon over 10 years
      No-one seems to have pointed out that you do not serialise a class, you serialise an object which represents an instance of a class. Perhaps that helps to see why it's useful?
  • oxbow_lakes
    oxbow_lakes about 14 years
    What on earth takes 10 minutes to build?
  • oxbow_lakes
    oxbow_lakes about 14 years
    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
  • David
    David about 14 years
    @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.
  • m_a_khan
    m_a_khan about 14 years
    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
    m_a_khan about 14 years
    So, it seems like it is just a better, more efficient way of writing data to a file and reading it back when needed?
  • Dirk Schumacher
    Dirk Schumacher almost 10 years
    @m_a_khan: Wow. Yes it can be done with a simple text. But as soon as Objects get more complex, or better their structrues (composation, inheritance) gets more complex it will become a hassle to manually (un)marshall them. Imagine having lists, sets and maps as object members.
  • David Soroko
    David Soroko almost 10 years
    It is easy to come up with different serialization schemes and indeed many exist. For very good reasons, none of them result in the general case, in a "simple text"
  • Gábor Bakos
    Gábor Bakos over 9 years
    In my opinion searialization and using a single config object as a parameter are quite orthogonal. Probably this is not a typical use case.
  • user3516726
    user3516726 over 8 years
    While attempting to put things in plain English, you haven't really explained serialization in any useful way.
  • Emily
    Emily almost 8 years
    This is the only REAL explanation . I can't think of any other real world application of serialization +1
  • Yakhoob
    Yakhoob over 6 years
    Short and sweet. Perfect explanation.
  • Vaibs
    Vaibs over 6 years
    If the web app request has to pass through each and every router in the globe before reaching the destination , building object, returning back with the object using the longest possible path, Yes it can take 10 minutes.
  • kidnan1991
    kidnan1991 almost 6 years
    In such case, why you need to store them in byte array stream (using serialize), could it be more simple than just using a temporary field?
  • Ashfaque Rifaye
    Ashfaque Rifaye over 5 years
    @Schmelter, Since you mention Serialization not best method for communication, which is the apt and best method to be implemented then?
  • Sebastian Nielsen
    Sebastian Nielsen over 4 years
    As far as I understand, everything inside the computer consists of bits (thus also bytes (a sequence of 8 bits)). If that is the case, isn't it already represented by a bytearray in the memory (RAM)? ..... So .. what is the point of turning something already represented by bytes into a bytearray?