File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Serialization/Deserialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Serialization/Deserialization" Watch "Serialization/Deserialization" New topic


Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 361
I am having a class which implement Serializable. I Serialize it on JVM. In the process of Serialization, the following occurs

Everytime an object is serialized the java serialization mechanism automatically computes a hash value. ObjectStreamClass's computeSerialVersionUID() method passes the class name, sorted member names, modifiers, and interfaces to the secure hash algorithm (SHA), which returns a hash value.The serialVersionUID is also called suid.
So when the serilaize object is retrieved , the JVM first evaluates the suid of the serialized class and compares the suid value with the one of the object. If the suid values match then the object is said to be compatible with the class and hence it is de-serialized. If not InvalidClassException exception is thrown.

Now if i Serialize using JVM1 and Deserialize using JVM2, then will the Deserialization work successfully. Also how does Serialization/Deserialization works across network, because when the object is transported across network the JVMs might be different.

SCJP 1.4, SCWCD1.4, OCA(1Z0-007)
Hrishikesh Yeshwant Alshi
Ranch Hand

Joined: Dec 06, 2010
Posts: 62

This is how serialization happens:

Java in memory object ---> Serialized ---> Written ---> Stream

Java in memory object <--- de-serialized <--- Read <--- Stream

Stream can be anything, like, output stream, error stream, file output stream or socket (as a network stream)

During writing and reading a protocol is followed which is independent of JVM. I mean, An object serialized by one JVM can be de-serialized by other JVM without any problem.

The process of writing the object on the network stream is called marshalling and reading from the network stream is called un-marshalling.

You can check out this site for details and diagrams.

Write your code as if the person who maintains it is a homicidal maniac who knows where you live.
Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 361
Thanks Hrishikesh. The article on Java serialization algorithm was very helpful.
I agree. Here's the link:
subject: Serialization/Deserialization
It's not a secret anymore!