Course Content

Course Content

Description

Serialization and Deserialization in Java

From our previous video tutorial we have seen that what are Multiple Catch Block in Java. Now in this video we are going to see that what is Serialization and Deserialization in Java programming language with accurate example.

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 in Java is called Deserialization in Java programming.

The String class and all the wrapper class implements java.io.Serializable interface by default.

Advantage of Java Serialization -
It is mainly used to travel object's state on the network (known as marshalling).

java.io.Serializable interface. Serializable is a marker interface (has no body). It is just used to "mark" java class which support a certain capability.

The Object Output Stream class is used to write primitive data types and Java objects to an Output Stream. Only objects that support the java.io.Serializable interface can be written to streams.

Object Constructor Output Stream (Output Stream out) throws IOException {} creates an Object Output Stream that writes to the specified Output Stream.

Following are the important methods of Serialization in Java with their description :
1. public final void write Object(Object obj) throws IOException {} : write the specified object to the object output stream.
2. public void flush() throws IOException {} : flushes the current output stream.
3. public void close() throws IOException {} : closes the current output stream.

The write Object() method of object output stream class provides the functionality to serialize the object. We save the state of the object in the file named f.txt.

Example :
import java.io.Serializable;
public class data implements Serializable
{
int id;
String name;
public data (int id, String name)
{
this.id=id;
this.name=name;
}
}
(Another package)
import java.io.*;
class Persist
{
public static void main (String args[]) throws Exception
{
data s1=new data (211, "ravi");
FileOutputStream fout=new FileOutputStream("f.txt"); ObjectOutputStream out=new
ObjectOutputStream (fout);
out.writeObject(s1);
out.flush();
System.out.println("success");
}
}
Output :
success

Deserialization in Java programming is the process of Converting the object from the serialized state to deserialized state in Java. It is the reverse operation of Serialization in Java.

An ObjectInputStream deserializes objects and primitive data written using an ObjectOutputStream.

public ObjectInputStream(InputStreamin) throws IOException {} : creates an ObjectInputStream that reads from the specified InputStream.

Following are the important methods of Deserialization with their description -
1. public final void write Object(Object obj) throws IOException {} : write the specified object to the object output stream.
2. public void flush() throws IOException {} : flushes the current output stream.
3. public void close() throws IOException {} : closes the current output stream.

Example :
import java.io.*;
class Depersist
{
public static void main (String args[]) throws Exception
{
ObjectInputStream in=new ObjectInputStream(new FileInputStream ("f.txt"));
data s=(data) in.readobject();
System.out.println(s.id+" "+s.name);
in.close();
}
}
Output :
211 ravi

So, in the process of Serialization in Java we serialise the state of our object, while in Deserialization in Java process we convert the Serialized state into the Object state. Now, in our next video we will see that what are Nested Try Catch Block in Java & Throw Keyword in Java Programming.

FAQs

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download The Apps

Code Scan or Download the app
Google Play Store
297K+ Downloads
4.5 Rating
10K+ Reviews
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere