2013-12-28

RSS from Javabeginnerstutorial.com

Here i will learn and teach you what is Serialization in Java and how to write code for the same. I am learning this because i am going for an Interview.

What is Serialization

Serialization is a process in which current state of Object will be saved in stream of bytes. As byte stream create is platform neutral hence once objects created in one system can be deserialized in other platform.

What is the use of Serialization

As written above serialization will translate the Object state to Byte Streams. This Byte stream can be used for different purpose.

Write to Disk

Store in Memory

Sent byte stream to other platform over network

Save byte stream in DB(As BLOB)

 

Serialization and Deserialization in Java

Now we know what is serialization. But in terms of Java how this serialization will work and how to make a class serializable. Java has already provided out of the box way(java.io.Serializable  Interface) to serialize an Object. If you want any class to be serialized then that class needs to implement give interface.

Note*: Serializable Interface is a Market Interface. Hence there is no method in Serializable interface.

 

Code for Serialization of Java Class

Employee.java

 

SerializaitonClass.java

 

DeserializationClass.java

First run “SerializaitonClass” and you will get “employee.txt” file created.

Second run “DeserializationClass” and java will deserialize the class and value will be printed in console.

Output would be

 

The post Java serialization concept and Example appeared first on Java Beginners Tutorial.

Show more