-1

This might look simple but i haven't got satisfying answer anywhere.

Why do we need Serialization ?

Answer I found everywhere is like -

To convert object in byte stream and to store in DB.

But my question is - can't we do it without using serialization? If not how we are storing the data in DB?

Please explain me clearly, if possible provide me an example

VasuDev
  • 31
  • 1
  • 8

2 Answers2

0

Serialization is not to store objects in a database.

It's to convert an object into a stream of bytes. That stream of bytes can be used indeed to store it into a database, but it can also be used to save it into a file or send it through a socket (here is an example).

Can't we do it without using serialization?

Absolutely, actually it's very rare to use serialization to store data in a database. Most of the times (I would say 99%) is using JDBC mainly through an ORM tool such as Hibernate.

OscarRyz
  • 196,001
  • 113
  • 385
  • 569
  • Thanks for response @OscarRyz, But my question is Even if i dont use serialization, Data/Object will be transferred in byte stream right? Then why serialization is important? – VasuDev May 09 '18 at 14:16
  • No, it won't be transferred into byte stream. There will be an in memory representation of the object, but that's not a "serial" version. That is, when you "serialize" an object, you put it one byte after another a linear sequence. Objects in memory are not layout like this and the actual representation is abstracted away from the user. Object serialization can be used when there's a need to represent the object into a stream of bytes. In some other cases you can use another data interchange format (json, xml, yaml, comma separated values) Serialization is just one of them. – OscarRyz May 09 '18 at 18:00
0

Because data is transferred as byte stream across the network, you cannot put your Object inside the wire.

In case of JDBC - sort of its own serialization performed by the Driver itself in appropriate format.

In general, this is not about programming, but general network structure.

The data traverses the following path:

  • Application layer
  • Transport layer
  • Internet layer
  • Link layer

At the last point it converted to the byte stream and physically crosses the network.

J-Alex
  • 6,881
  • 10
  • 46
  • 64
  • 1
    You don't serialize anything when using JDBC, with JDBC you explicitly say: "For this table field, use this value" so you deconstruct the object manually. – OscarRyz May 09 '18 at 18:01