Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Byte Code vs Stream of bytes(Serialization) [duplicate]

Tags:

I have a question regarding byte code and serialization.

bytecode - every java class is converted to bytecode for compilation and stored on the memory (disk) as stream of bytes / bytecode.

Serialization - Serialization is the process of saving an object's state to a sequence of bytes.

can't this bytecode itself be used to send through network ?

So what is the exact difference between the two ??

Thanks in advance.

like image 631
Prat Avatar asked Jun 15 '16 07:06

Prat


1 Answers

The concepts are completely unrelated.

every java class is converted to bytecode for compilation

No, it isn't converted for compilation. Bytecode is the result of compilation. It is, well, code which is run by a virtual machine (JVM in Java's case) like machine code is run directly by the CPU. It is binary (a sequence of bytes instead of characters) simply because it's a more compact representation. It doesn't contain state of any objects (except for constants).

Serialization stores the state of an object (default Java serialization is binary, but other libraries/languages can use text). The serialized object doesn't contain bytecode: you can't run it or in JVM.

like image 63
Alexey Romanov Avatar answered Sep 28 '22 02:09

Alexey Romanov