Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

i wonder what is difference between object and stream of bytes?


when to use serialzation and deserialization?

why we have to convert to stream of bytes to store into a database?

why we cannot store object itself to a database without converting to stream of bytes?

please advise
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

An Object is a piece of code plus some values stored in RAM.

A database stores things on disk.

So you need some way to convert between them.  One way to do that is serialization - converting the Object into a string of characters (a stream of bytes) so they can be stored as any other String.

So your Object like this:
public class MyClass {
   private final String m_Name ;
   private final int m_Count ;

MyClass myObject = new MyClass("Hello", 20) ;
becomes "0FB23478CED0123123"

which can then be stored and retrieved from a database or written to disk.

That's serialization.

De-serialization is just going the other way.
Converting "0FB23478CED0123123"
back to an object like this:
MyClass myObject = new MyClass("Hello", 20) ;

Any clearer?

An object is an instance of a class.

Suppose the class is defined (say, call it Employees) in both a database server program and a remote GUI program.
In both, an instance of this Employees class is created.

Assume that a remote GUI sends a message to a database server program which in turn queries the database and populates the Employees instance data members. The GUI program would like a verbatim replica of the database server program's Employees instance data members. One way to do that to create a message whose fields correspond to the Employees instance data members and send the message over a socket.

But that's a lot of programming work if there are lot of different queries.

Instead, the database server program just serializes the Employees instance and sends this serialized object over the socket. The GUI receives it and de-serializes this serialized object into its own Employees instance object. Now the GUI has a replica of the database server program corresponding instance.

This approach is much easier to program. One issue to consider is keeping the remote GUI and the database server program in sync. If you were to add a field to the Employees class, you better have the new definition on all the programs that use this class.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.