What is the purpose of Serialization in Java?
Asked Answered
W

9

117

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?

Wonderful answered 9/2, 2010 at 21:45 Comment(11)
Do you mean serializing classes, or objects?Separative
He "wasn't convinced" they answered his questions.Deceleron
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.Rios
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.Writ
@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.Wonderful
If we look at this question: stackoverflow.com/questions/1941775/… You got an answer which answered the question posted, and just ignored it.Rios
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.Adila
@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?Wonderful
Either: (Accept the answer) or (Post a comment and/or amend the question explaining why it's not what you need).Rios
Its better explain here : vogella.com/articles/JavaSerialization/article.htmlDialyse
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?Gittle
S
203

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.

Seminarian answered 9/2, 2010 at 21:56 Comment(6)
What on earth takes 10 minutes to build?Sordino
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 changesSordino
@Sordino 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.Writ
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.Vestige
@Schmelter, Since you mention Serialization not best method for communication, which is the apt and best method to be implemented then?Calcariferous
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?Treat
T
72

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.

Trivial answered 9/2, 2010 at 21:55 Comment(3)
So, it seems like it is just a better, more efficient way of writing data to a file and reading it back when needed?Wonderful
This is the only REAL explanation . I can't think of any other real world application of serialization +1Lundeen
Short and sweet. Perfect explanation.Beforehand
C
21

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.

Carine answered 9/10, 2014 at 16:10 Comment(1)
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?Zoophilous
A
5

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.
Abnaki answered 9/2, 2010 at 21:49 Comment(0)
L
3

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.

Loppy answered 9/2, 2010 at 21:48 Comment(0)
E
0

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.

Evaporation answered 9/2, 2010 at 21:51 Comment(3)
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?Wonderful
@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.Confederacy
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"Evaporation
T
0

I use serialized objects to standardize the arguments I pass to functions or class constructors. Passing one serialized bean is much cleaner than a long list of arguments. The result is code that is easier to read and debug.

Traynor answered 22/11, 2014 at 15:39 Comment(1)
In my opinion searialization and using a single config object as a parameter are quite orthogonal. Probably this is not a typical use case.Adagietto
F
0

For the simple purpose of learning (notice, I said learning, I did not say best, or even good, but just for the sake of understanding stuff), you could save your data to a text file on the computer, then have a program that reads that info, and based on the file, you could have your program respond differently. If you were more advanced, it wouldn't necessarily have to be a txt file, but something else.

Serializing on the other hand, puts things directly into computer language. It's like you're telling a Spanish computer something in Spanish, rather than telling it something in French, forcing it to learn French, then save things into its native Spanish by translating everything. Not the most tech-intensive answer, I'm just trying to create an understandable example in a common language format.

Serialization is also faster, because in Java, objects are handled on the heap, and take much longer than if they were represented as primitives on the stack. Speed, speed, speed. And less file processing from a programmer point of view.

Fosterling answered 26/3, 2015 at 21:31 Comment(1)
While attempting to put things in plain English, you haven't really explained serialization in any useful way.Sigvard
K
0

One of the classical example where serialization is used in daily life is "Save Game" option in any computer games. When player decides save his progress in the game then the application writes the saved state of the game into a file via serialization and when player "Load Game" the serialized file is read and Game state is re-created.

Keciakeck answered 28/8, 2022 at 10:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.