Reputation: 5771
I have read the definition of serialization in many a places. They all seem to say that it is a way to convert an object (in a OOP language like Java or Python) to a byte stream.
Aren't all objects already present as a byte stream at the most basic level? Or when we save anything to any disk, is not everything already just 1's and 0's that gets saved and is that that gets read later on? What isn't a byte stream? What is converting the objects to their corresponding byte stream all about?
Upvotes: 4
Views: 74
Reputation: 138
Aren't all objects already present as a byte stream at the most basic level?
No. (a) They're likely "all over the place", and (b) there's no particular requirement that one implementation equal another.
Consider
class MyClass {
String s = "Hello";
}
MyClass thing = new MyClass();
OK, so variable 'thing' points to an instance of MyClass. The instance (obviously) occupies memory somewhere.
That instance contains only (ignoring implementation-private pieces) a reference to a String object.
The referenced String object is elsewhere. It (probably; the implementation is opaque to us users) contains a reference to some characters.
The referenced characters are elsewhere; probably in static storage because they never change.
So, there is no "byte stream". The instance of MyClass is not a single piece of contiguous memory. Serialization is what collects all these separate pieces into a byte stream in a standardized way.
Upvotes: 5
Reputation: 5568
Yes, every object in your program is already represented by some kind of bits somewhere in memory.
But in the general case you can not just take these bits, copy them to another process and expect them to work.
This representation is highly dependent on (a) current state of the JVM and (b) what version of Java you are running, and (c) if the object is compiled by the JIT. Some bits might be shared among several objects. It is not necessary that all bits are next to each other. Some might be swapped to some hard drive. For optimization reasons there might be multiple copies of the same object in slightly different state (One in processor cache, one in 2nd level cache, one in RAM, one on disk).
Serialization gives you a well defined bit representation of an object graph, so you can actually use it in a different process.
Upvotes: 6