Short story about serialization

After many years of hard work, Earth's scientists developed a robot who can help them in daily work. But this robot had fewer features than the robots developed by the scientists from planet Mars.

After a meeting between both planets' scientists, it is decided that Mars will send their robots to Earth. But a problem occurred. The cost of sending 100 robots to Earth was $100 million. And it takes around 60 days of traveling.

Finally, Mars' scientists decided to share their secret with Earth's scientists. This secret was about the structure of class/robot. Earth's scientists developed the same structure on Earth itself. Mars' scientists serialized the data of each robot and sent it to earth. Earth's scientists deserialized the data and fed it into each robot accordingly.

This process saved them time in communicating a massive amount of data.

Some of the robots were being used in some defensive work on Mars. So their scientists marked some crucial properties of those robots as transient before sending their data to Earth. Note that the transient property is set to null (in case of reference) or to the default value (in case of the primitive type) when the object gets deserialized.

One more point noticed by Earth's scientists is that Mars' scientists asked them to create some static variables to keep details about the environment. These details are used by some robots. But Mars' scientists don't share these details. Because Earth's environment was different from Mars' environment.

Even though knowing about the robot class structure and having serialized data Earth's scientist were not able to deserialize the data which can make robots working.

Exception in thread "main" java.io.InvalidClassException:
SerializeMe; local class incompatible: stream classdesc
:

Mars' scientists were waiting for the complete payment. Once the payment was done Mars' scientists shared the serialversionUID with Earth's scientists. Earth's scientist set it to robot class and everything started working.

Update

Though with the help of serialization, they became able to send data using signals instead of actual spaceship, they realized that sending large set of data was still a challenge. Serialization make the process cheaper and faster but it was still slow. Hence the different scientists came up with different ideas to reduce the data size. Some scientists suggested to compress the data and some suggested to use different mechanism to represent it so it can be deserialized back. Some of the ideas are XML, JSON, msgpack, Nimn

Answer from Amit Kumar Gupta on Stack Overflow
Top answer
1 of 9
209

Short story about serialization

After many years of hard work, Earth's scientists developed a robot who can help them in daily work. But this robot had fewer features than the robots developed by the scientists from planet Mars.

After a meeting between both planets' scientists, it is decided that Mars will send their robots to Earth. But a problem occurred. The cost of sending 100 robots to Earth was $100 million. And it takes around 60 days of traveling.

Finally, Mars' scientists decided to share their secret with Earth's scientists. This secret was about the structure of class/robot. Earth's scientists developed the same structure on Earth itself. Mars' scientists serialized the data of each robot and sent it to earth. Earth's scientists deserialized the data and fed it into each robot accordingly.

This process saved them time in communicating a massive amount of data.

Some of the robots were being used in some defensive work on Mars. So their scientists marked some crucial properties of those robots as transient before sending their data to Earth. Note that the transient property is set to null (in case of reference) or to the default value (in case of the primitive type) when the object gets deserialized.

One more point noticed by Earth's scientists is that Mars' scientists asked them to create some static variables to keep details about the environment. These details are used by some robots. But Mars' scientists don't share these details. Because Earth's environment was different from Mars' environment.

Even though knowing about the robot class structure and having serialized data Earth's scientist were not able to deserialize the data which can make robots working.

Exception in thread "main" java.io.InvalidClassException:
SerializeMe; local class incompatible: stream classdesc
:

Mars' scientists were waiting for the complete payment. Once the payment was done Mars' scientists shared the serialversionUID with Earth's scientists. Earth's scientist set it to robot class and everything started working.

Update

Though with the help of serialization, they became able to send data using signals instead of actual spaceship, they realized that sending large set of data was still a challenge. Serialization make the process cheaper and faster but it was still slow. Hence the different scientists came up with different ideas to reduce the data size. Some scientists suggested to compress the data and some suggested to use different mechanism to represent it so it can be deserialized back. Some of the ideas are XML, JSON, msgpack, Nimn

2 of 9
159

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.

Serialization is the translation of your Java object's values/states to bytes to send it over network or save it.

This is analogous to how your voice is transmitted over PSTN telephone lines.

🌐
CodeJava
codejava.net › java-se › file-io › why-do-we-need-serialization-in-java
Why Do We Need Serialization in Java?
Serializable is a marker interface (contains no methods) that tell the Java Virtual Machine (JVM) that the objects of this class is ready for being written to and read from a persistent storage or over the network.By default, the JVM takes care of the process of writing and reading serializable ...
Discussions

Explain like i'm five - what is Serializable?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
9
24
April 26, 2024
Java serialization - advantages and disadvantages, use or avoid? - Software Engineering Stack Exchange
In all other situations "Binary Java serialization" is bad ... Protected question. To answer this question, you need to have at least 10 reputation on this site (not counting the association bonus). The reputation requirement helps protect this question from spam and non-answer activity. More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
Can someone please help clarify exactly why Serialization is necessary and how it is implemented in Java.
Donnie Turner is having issues with: I am currently working in "Java Data Structures" with Craig Dennis and would appreciate a little more clarity on Serialization and its purpose. ... More on teamtreehouse.com
🌐 teamtreehouse.com
3
August 17, 2016
Why should I use kotlinx.serialization?
One of the reasons why kotlinx.serialization doesn't serialize anything you throw at it (like how Gson works) is because kotlinx.serialization doesn't rely on Reflection to find out what fields should be serialized, the serializer/deserializer is generated during compilation, which is why it requires a compiler plugin. This also means that you end up needing to create your own serializer/deserializers for any class that isn't @Serializable. More on reddit.com
🌐 r/Kotlin
29
37
February 24, 2023
Top answer
1 of 10
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.

2 of 10
74

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.

🌐
Reddit
reddit.com › r/javahelp › explain like i'm five - what is serializable?
r/javahelp on Reddit: Explain like i'm five - what is Serializable?
April 26, 2024 -

I just don't get it. I'm a junior and see it often in the codebase of the company i work at. Documentation says that it helps serialize and deserialize objects, but why does that need to happen using this interface? There are so many classes that do not implement Serializable, so what happens to them?
Head First Java book says that objects need to be serialized when data is sent over the network or saved to a disk. But there is serialization/deserialization happening to JSON objects for example when they're being sent from server to client and vice versa, and those classes do not implement Serializable.
So in which "special" scenario does one need/want to implement Serializable?

🌐
Quora
quora.com › Why-is-serialization-required-in-Java
Why is serialization required in Java? - Quora
Answer (1 of 15): With the ever-increasing boom of the IT industry, our reliance on it seems to be growing with every passing day. In a bid to stay relevant within the industry, steady growth has pushed many working professionals to take up programming languages. While the industry makes use of s...
🌐
Quora
quora.com › Why-and-when-do-we-use-serialization-in-Java
Why and when do we use serialization in Java? - Quora
Answer: We use serialization when we want to convert an object into a sequence of bytes. This is useful when we want to save an object in a file or when we want to send an object through some streams or over the network via sockets which we can do only via serialization. In java, to make an objec...
🌐
Baeldung
baeldung.com › home › java › core java › introduction to java serialization
Introduction to Java Serialization | Baeldung
May 11, 2024 - Serialization is the conversion of the state of an object into a byte stream; deserialization does the opposite. Stated differently, serialization is the conversion of a Java object into a static stream (sequence) of bytes, which we can then save to a database or transfer over a network.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › serialization-and-deserialization-in-java
Serialization and Deserialization in Java - GeeksforGeeks
June 1, 2026 - The serialization process converts a Java object into a byte stream, allowing it to be stored or transmitted while preserving its state. Since the generated byte stream is platform-independent, an object serialized on one platform can be ...
🌐
Edureka
edureka.co › blog › serialization-in-java
What is the concept of Serialization in Java? | Edureka
June 17, 2021 - Serialization in Java is an important concept that deals with the conversion of objects into a byte stream to transport the java objects from one Java Virtual Machine to the other and recreate them to the original form.
🌐
Net Informations
net-informations.com › java › cjava › purpose.htm
What is the purpose of serialization/deserialization in Java?
Serialization's ability to bridge ... Serialization is used in Java to enable the saving, transmission, and sharing of object state across different machines or persistent storage, ensuring data integrity and facilitating interoperability between systems....
🌐
Medium
medium.com › but-it-works-on-my-machine › serialization-in-java-do-we-really-need-it-6c10fa18ca27
Serialization in Java: Do We Really Need It? | by Ahmet Emre DEMİRŞEN | @Override | Medium
March 12, 2026 - At its heart, serialization in Java is the process of converting an object’s state into a byte stream. The reverse, converting a byte stream back into an object, is called deserialization. We need this because storage mediums like files or network connections deal with bytes, not Java objects.
Top answer
1 of 6
19

Serialization is mostly used in two areas:

  • prototyping of persistence

    pretty much every object graph can quickly be made serializable, for quick proof-of-concepts or quick-and-dirty applications this might be faster than setting up a real ORM layer or other persistence system

  • short term storage of almost-arbitrary objects:

    Applications servers, for example, have a tendency to persist session information using serialization. This has the advantage that the values in the session can be almost any type (as long as its serializable).

For almost all other uses, the drawbacks you (and the article) mentions are too big: the exact format is hard to keep stable, class changes can easily make your serialized data unreadable, reading/writing the data in non-Java code is almost impossible (or at least a lot harder than necessary).

JAXB and similar technologies provide similar functions with a similarly low cost, while reducing some of the problems.

2 of 6
13

I use object serialization to allow post-mortem analysis in case of an unexpected error in production. The inputs to a calculation are serialized to a data file. If an error is reported, a simple program can reload the inputs and rerun the calculation with a debugger attached. Or a groovy shell can be used to reload the objects and modify them if desired.

We also use serialization to pass Java objects through HTTP to a web service. Much easier than serializing to and from text. The disadvantage is that the client and server installations must be deployed together, but that's not problem since we control both ends.

🌐
DigitalOcean
digitalocean.com › community › tutorials › serialization-in-java
Serialization in Java - Java Serialization | DigitalOcean
August 3, 2022 - Serialization in Java allows us to convert an Object to stream that we can send over the network or save it as file or store in DB for later usage. Deserialization is the process of converting Object stream to actual Java Object to be used in our program. Serialization in Java seems very easy ...
🌐
Simplilearn
simplilearn.com › home › resources › software development › understanding serialization in java with examples
Serialization in Java: Mechanism, Benefits & Examples
June 9, 2025 - Learn about serialization in Java and how to serialize objects with examples. Explore its benefits, deserialization, and key Java concepts in this guide.
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
SATHEE
sathee.iitk.ac.in › home › article › miscellaneous › serialization in java- exploring the importance advantages and disadvantages
SATHEE: Serialization In Java- Exploring The Importance Advantages And Disadvantages
It is the process of converting a complex, live object into a format that can be easily stored, transported, or reconstructed. In this section, we will explore the art and science of Java object serialization, exploring the steps and techniques required to encapsulate an object’s state and behaviour into a byte stream, making it portable and persistently retrievable.
🌐
Careers360
careers360.com › home › online courses & certifications › articles
Serialization In Java: Exploring The Importance, Advantages And Disadvantages
May 20, 2025 - It is the process of converting a complex, live object into a format that can be easily stored, transported, or reconstructed. In this section, we will explore the art and science of Java object serialization, exploring the steps and techniques required to encapsulate an object's state and behaviour into a byte stream, making it portable and persistently retrievable.
🌐
Oracle
oracle.com › java › technical details
Discover the secrets of the Java Serialization API
However, all of those objects exist only as long as the Java virtual machine1 remains running. It would be nice if the objects we create could exist beyond the lifetime of the virtual machine, wouldn't it? Well, with object serialization, you can flatten your objects and reuse them in powerful ways. Object serialization is the process of saving an object's state to a sequence of bytes, as well as the process of rebuilding those bytes into a live object at some future time.
🌐
DZone
dzone.com › coding › languages › everything you need to know about java serialization explained
Everything You Need to Know About Java Serialization Explained
August 14, 2019 - Java cloning is the most debatable topic in Java community and it surely does have its drawbacks but it is still the most popular and easy way of creating a copy of an object until that object is full filling mandatory conditions of Java cloning. I have covered cloning in details in a 3 article long Java Cloning Series which includes articles like Java Cloning And Types Of Cloning (Shallow And Deep) In Details With Example, Java Cloning - Copy Constructor Versus Cloning, Java Cloning - Even Copy Constructors Are Not Sufficient, go ahead and read them if you want to know more about cloning. Serialization is the process of saving an object's state to a sequence of bytes, which then can be stored on a file or sent over the network, and deserialization is the process of reconstructing an object from those bytes.
🌐
Team Treehouse
teamtreehouse.com › community › can-someone-please-help-clarify-exactly-why-serialization-is-necessary-and-how-it-is-implemented-in-java
Can someone please help clarify exactly why Serialization is necessary and how it is implemented in Java. (Example) | Treehouse Community
August 17, 2016 - "Java provides a mechanism, called object serialization where an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object.
🌐
Upgrad
upgrad.com › home › blog › software development › serialization in java: everything you need to know [2024]
Serialization in Java: Everything You Need To Know [2024] | upGrad blog
February 24, 2025 - This is because data streams that are serialized support encryption, authentication, compression, and secure java computing. No programming language is a hundred per cent perfect, and neither can they claim to be.