Wednesday, 18 November 2015

What is Serialization and Deserialization? Give me examples

Serialization : Serialization in java is a mechanism of writing the state of an object into a byte stream.It is mainly used in Hibernate, RMI, JPA, EJB, JMS technologies. The reverse operation of serialization is called deserialization. The String class and all the wrapper classes implements java.io.Serializable interface by default.

Advantages: It is mainly used to travel object's state on the network (known as marshaling).

Example:

Deserialization : Deserialization is the process of reconstructing the object from the serialized state.It is the reverse operation of serialization.
Example:
More Explanation:

1.
Banking example: When the account holder tries to withdraw money from the server through ATM, the account holder information along with the withdrawl details will be serialized (marshalled/flattened to bytes) and sent to server where the details are deserialized (unmarshalled/rebuilt the bytes)and used to perform operations. This will reduce the network calls as we are serializing the whole object and sending to server and further request for information from client is not needed by the server.

2.
Stock example: Let’s say an user wants the stock updates immediately when he request for it. To achieve this, everytime we have an update, we can serialize it and save it in a file. When user requests the information, deserialize it from file and provide the information. This way we don’t need to make the user wait for the information until we hit the database, perform computations and get the result.

Why we need Serialization: To persist data for future use.

No comments:

Post a Comment