0

I'm trying to understand the difference between serialization and deserialization in java. I know lots of answers and topics are available online but I didnt find any suitable to understand.

Here are my few doubts:

1) Object sits in a memory in a de-serialized form, but how it looks ? Doesnt it stores in bytes form(0 and 1) only ? Or any other form ?

2) Once the objects gets serialized, its a stream of bytes, then how it looks or different from de-serialized form (step 1) ?

3) When we send object's data across the network, which form converts into bytes ?

I know it could be very silly doubts, but please bear with me and put me in right direction .! :)

1
  • can you explain what you mean by 3rd point?
    – GotoFinal
    Jul 13, 2018 at 13:44

1 Answer 1

1
  1. It might look differently depending on version of Java, your PC (like different byte order, different object header size on 64/32 bit systems - or even JVM flags), and just everything. It also contains a lot of data we don't need when serializing, like information about garbage collection, locks, internal/system hash code etc. You can learn more by looking for object header: What is in java object header
    But after all object is just some bytes in memory.

  2. Main goal of serialization is to create some standardised form - so something that you can later easily read back without dependency on your architecture, system, and without data you don't need like GC one.
    This can be some raw binary format, like default java serialization, or some binary json - or some text based formats like json, yaml, xml. (text is bytes too, but for sure much easier to display and read by humans)
    Also read this question for more information about serialization itself: What is serialization?

  3. This is your choice - you are sending that object, so you are the one that need to serialize it, in some languages (in java too if you will use some unsafe magic) it is possible to just send same data as in memory, but in most cases this is a really bad idea. So when sending data over network you should serialize it first.

2
  • " it is possible to just send same data as in memory, but in most cases this is a really bad idea." Why ?
    – Elena
    Jul 13, 2018 at 14:28
  • @Elena because then you might be unable to use it, because you will send it to PC that expect different architecture. In java I'm not sure if it would be possible to use such object at all after injecting it to memory - never tried to do something that weird
    – GotoFinal
    Jul 13, 2018 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.