1

I'm trying to undertsand what Serialization and De-Serialization means in Java.

I guess every thing we write in Java language becomes set of bytes when code gets compiled and goes to processor to get execute. After compilation, every thing is turned into machine language or bytes. Correct ..?

So object creation is already a set of bytes which sits in memory to be worked, then what does Serialize and Non-Serialize terms do special to object .. ?

I'm not clear to imagine these two terms in computer hardware..!

Can anyone help ..?

Thanks

Jon
  • 89
  • 7
  • 1
    Why downvote to my question ..? – Jon Jul 07 '19 at 07:03
  • 1
    Serialization means to turn an object in to serial data (string) like JSON or XML. Deserialization means to turn it from a string back into an object – jrswgtr Jul 07 '19 at 07:05
  • @jrswgtr, but string and object both are already a set of bytes ..isn't it ..? Then why we need string over object ? – Jon Jul 07 '19 at 07:07
  • Both *program* and *data* take the form of bytes (start by understanding this). *Serialization* is said about data (objects) from the perspective of an already running program. Objects are created by your program when it runs. When those objects are written as bytes in the form suitable for disks (or sometimes memory too), they're said to be serialized (and 'deserialized' when they're read off those bytes into a form ready for use by the running program). – ernest_k Jul 07 '19 at 07:07
  • @Jon say you want to send the object over HTTP. Then you will need a serialized object to do that because HTTP can't handle objects but only strings – jrswgtr Jul 07 '19 at 07:09
  • @Slaw, What ever type it is in java whether its Object, String, Integer, int etc..everything is a set of bits but in different forms.Its a only difference is there that's what I think. So you mean to say when every its required to send object over network, object bits (for example 001) gets converted into String (for example 100)..that's what java does ...? AM I thinking WRONG ..? – Jon Jul 07 '19 at 07:16
  • I think the question is pointed to the terms of Java serialization and the `Serializable` interface – Royal Bg Jul 07 '19 at 07:18
  • The difference between a serialized object and a non-serialized object is that nobody has serialized the latter yet. Do you mean *Serializable* and non-Serializable? – user207421 Jul 07 '19 at 07:20

1 Answers1

4

Serialization is a persistence mechanism.

You use it at runtime to turn "in memory" objects into a representation that can be stored in a file or database, or that can be sent over some network.

And deserialisation is simply the reverse operation: turning that "transport" representation back into a "in memory" object.

The main reason for this: it allows that "in memory" representation to be whatever makes sense. As outlined in one of the comments: it isn't necessarily true that Java objects exist within one continuous section of memory. But when you want to "pass" such objects to a persistence layer, or to another programing language, it is essential to have a such a sequential representation of the (field) data that makes up the Java object.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • 2
    Great answer. I would like to add to this: as @Jon states: "Why use serialization if the object in memory is already a set of bytes?". That is off course because serialization is also meant to be used to "communicate" with other programming languages that can't understand Java's format of "a set of bytes". (maybe you can put this better:-p).. Something about abstraction... – jrswgtr Jul 07 '19 at 08:13
  • 2
    One way to distinguish the "in memory" object from a serialized object is that the "in memory" object can be spread out over multiple areas in memory (because slightly over-simplified each Java Object is a separate area and a `String` for example is made up of the fields of the `String` in one area and an associated `char[]` in another area). The *serialized* form is one consecutive chunk of bytes: everything is in one easy to handle area. If you pass that around, you'll pass everything you need about the String. – Joachim Sauer Jul 07 '19 at 09:10
  • 1
    @JoachimSauer very true, I reworked my answer a bit along your comment! Thanks! – GhostCat Jul 07 '19 at 09:32
  • 1
    @jrswgtr thanks, too! It is always great to come back to find such helpful constructive comments! – GhostCat Jul 07 '19 at 09:34