Serialization: Serialization: (1.1 V) De-Serialization
Serialization: Serialization: (1.1 V) De-Serialization
nd nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
201 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
202 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Serialization
Agenda :
1. Serialization
2. Deserialization
3. transient keyword
4. static Vs transient
5. transient Vs final
6. Object graph in serialization.
7. customized serialization.
8. Serialization with respect inheritance.
9. Externalization
10. Difference between Serialization & Externalization
11. SerialVersionUID
Serialization: (1.1 v)
1. The process of saving (or) writing state of an object to a file is called serialization
2. but strictly speaking it is the process of converting an object from java supported De-Serialization:
form to either network supported form (or) file supported form.
3. By using FileOutputStream and ObjectOutputStream classes we can achieve 1. The process of reading state of an object from a file is called DeSerialization
serialization process. 2. but strictly speaking it is the process of converting an object from file supported
4. Ex: big ballon form (or) network supported form to java supported form.
3. By using FileInputStream and ObjectInputStream classes we can achieve
DeSerialization.
Diagram:
nd nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
203 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
204 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR JAVA Means DURGA SIR
Diagram: Diagram:
Example 1:
import java.io.*;
class Dog implements Serializable
{
int i=10;
int j=20; Note:
}
classSerializableDemo 1. We can perform Serialization only for Serilizable objects.
{
public static void main(String args[])throws Exception{ 2. An object is said to be Serilizable if and only if the corresponding class
Dog d1=new Dog(); implements Serializable interface.
System.out.println("Serialization started"); 3. Serializable interface present in java.io package and does not contain any
FileOutputStreamfos=new FileOutputStream("abc.ser"); methods. It is marker interface. The required ability will be provided
ObjectOutputStreamoos=new ObjectOutputStream(fos);
automatically by JVM.
oos.writeObject(d1);
System.out.println("Serialization ended"); 4. We can add any no. Of objects to the file and we can read all those objects from
System.out.println("Deserialization started"); the file but in which order we wrote objects in the same order only the objects
FileInputStreamfis=new FileInputStream("abc.ser"); will come back. That is order is important.
ObjectInputStreamois=new ObjectInputStream(fis); 5. If we are trying to serialize a non-serializable object then we will get
Dog d2=(Dog)ois.readObject();
System.out.println("Deserialization ended");
RuntimeException saying "NotSerializableException".
System.out.println(d2.i+"................"+d2.j);
}
}
Output:
Serialization started
Serialization ended
Deserialization started
Deserialization ended
10................20
nd nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
205 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
206 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Example2:
import java.io.*;
class Dog implements Serializable
{
int i=10;
int j=20;
}
class Cat implements Serializable
{
int i=30;
int j=40;
}
Static VsTransient :
classSerializableDemo
{ 1. static variable is not part of object state hence they won't participate in
public static void main(String args[])throws Exception{ serialization because of this declaring a static variable as transient there is no
Dog d1=new Dog(); use.
Cat c1=new Cat();
System.out.println("Serialization started");
FileOutputStreamfos=new FileOutputStream("abc.ser");
ObjectOutputStreamoos=new ObjectOutputStream(fos);
oos.writeObject(d1); Transient Vs Final:
oos.writeObject(c1);
System.out.println("Serialization ended");
System.out.println("Deserialization started");
1. final variables will be participated into serialization directly by their values.
FileInputStreamfis=new FileInputStream("abc.ser"); Hence declaring a final variable as transient there is no use.
ObjectInputStreamois=new ObjectInputStream(fis); //the compiler assign the value to final variable
Dog d2=(Dog)ois.readObject();
Cat c2=(Cat)ois.readObject(); Example 3:
System.out.println("Deserialization ended"); import java.io.*;
System.out.println(d2.i+"................"+d2.j); class Dog implements Serializable
System.out.println(c2.i+"................"+c2.j); {
} static transient int i=10;
} final transient int j=20;
Output: }
Serialization started classSerializableDemo
Serialization ended {
Deserialization started public static void main(String args[])throws Exception{
Deserialization ended Dog d1=new Dog();
10................20 FileOutputStreamfos=new FileOutputStream("abc.ser");
30................40 ObjectOutputStreamoos=new ObjectOutputStream(fos);
oos.writeObject(d1);
Transient keyword: FileInputStreamfis=new FileInputStream("abc.ser");
ObjectInputStreamois=new ObjectInputStream(fis);
Dog d2=(Dog)ois.readObject();
1. transient is the modifier applicable only for variables. System.out.println(d2.i+"................"+d2.j);
nd nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
207 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
208 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR JAVA Means DURGA SIR
}
transient final int i=10;
} 10................20
Output: transient static int j=20;
10................20
Diagram:
We can serialize any no of objects to the file but in which order we serialized in the
same order only we have to deserialize.
Example :
FileOutputStreamfos=new FileOutputStream("abc.ser");
ObjectOutputStreamoos=new ObjectOutputStream(fos);
oos.writeObject(d1);
oos.writeObject(c1);
oos.writeObject(r1);
FileInputStreamfis=new FileInputStream("abc.ser");
ObjectInputStreamois=new ObjectInputStream(fis);
Table: Dog d2=(Dog)ois.readObject();
Cat c2=(Cat)ois.readObject();
declaration output Rat r2=(Rat)ois.readObject();
int i=10;
10................20 If we don't know order of objects :
int j=20;
transient int i=10; Example :
0................20
int j=20;
transient int i=10; FileInputStreamfis=new FileInputStream("abc.ser");
0................20 ObjectInputStreamois=new ObjectInputStream(fis);
transient static int j=20; Object o=ois.readObject( );
transient final int i=10;
10................0 if(o instanceof Dog) {
transient int j=20;
Dog d2=(Dog)o;
nd nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
209 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
210 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Example 4:
import java.io.*;
class Dog implements Serializable
{
Cat c=new Cat();
}
class Cat implements Serializable
{
Rat r=new Rat();
}
class Rat implements Serializable
{
int j=20;
}
classSerializableDemo
nd nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
211 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
212 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR JAVA Means DURGA SIR
Diagram: In the above example whenever we are serializing Dog object automatically Cat
and Rat objects will be serialized because these are part of object graph of Dog
object.
Among Dog, Cat, Rat if at least one object is not serializable then we will get
runtime exception saying "NotSerializableException".
Customized serialization:
During default Serialization there may be a chance of lose of information due to
transient keyword.(Ex : mango ,money , box)
Example 5:
import java.io.*;
class Account implements Serializable
{
String userName="Bhaskar";
transient String pwd="kajal";
}
classCustomizedSerializeDemo
{
public static void main(String[] args)throws Exception{
Account a1=new Account();
System.out.println(a1.userName+"........."+a1.pwd);
FileOutputStreamfos=new FileOutputStream("abc.ser");
ObjectOutputStreamoos=new ObjectOutputStream(fos);
oos.writeObject(a1);
FileInputStreamfis=new FileInputStream("abc.ser");
ObjectInputStreamois=new ObjectInputStream(fis);
Account a2=(Account)ois.readObject();
System.out.println(a2.userName+"........."+a2.pwd);
}
}
Output:
Bhaskar.........kajal
Bhaskar.........null
Diagram:
nd nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
213 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
214 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Example 6:
Demo program for customized serialization to recover loss of information which is
happen due to transient keyword.
import java.io.*;
class Account implements Serializable
{
String userName="Bhaskar";
transient String pwd="kajal";
private void writeObject(ObjectOutputStreamos)throws Exception
{
os.defaultWriteObject();
String epwd="123"+pwd;
os.writeObject(epwd);
}
private void readObject(ObjectInputStream is)throws Exception{
is.defaultReadObject();
String epwd=(String)is.readObject();
pwd=epwd.substring(3);
}
}
classCustomizedSerializeDemo
{
public static void main(String[] args)throws Exception{
Account a1=new Account();
System.out.println(a1.userName+"........."+a1.pwd);
In the above example before serialization Account object can provide proper FileOutputStreamfos=new FileOutputStream("abc.ser");
username and password. But after Deserialization Account object can provide ObjectOutputStreamoos=new ObjectOutputStream(fos);
oos.writeObject(a1);
only username bur not password. This is due to declaring password as transient. FileInputStreamfis=new FileInputStream("abc.ser");
Hence doing default serialization there may be a chance of loss of information ObjectInputStreamois=new ObjectInputStream(fis);
due to transient keyword. Account a2=(Account)ois.readObject();
We can recover this loss of information by using customized serialization. System.out.println(a2.userName+"........."+a2.pwd);
}
}
We can implements customized serialization by using the following two methods.
Output:
Bhaskar.........kajal
Bhaskar.........kajal
1. private void writeObject(ObjectOutputStreamos) throws Exception.
nd nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
215 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
216 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR JAVA Means DURGA SIR
If parent class implements Serializable then automatically every child class by default
implements Serializable. That is Serializable nature is inheriting from parent to child.
Hence even though child class doesn't implements Serializable , we can serialize child
class object if parent class implements serializable interface.
Example 7:
import java.io.*;
class Animal implements Serializable
{
int i=10;
}
Diagram: class Dog extends Animal
{
int j=20;
}
classSerializableWRTInheritance
{
public static void main(String[] args)throws Exception{
Dog d1=new Dog();
System.out.println(d1.i+"........"+d1.j);
FileOutputStreamfos=new FileOutputStream("abc.ser");
ObjectOutputStreamoos=new ObjectOutputStream(fos);
oos.writeObject(d1);
FileInputStreamfis=new FileInputStream("abc.ser");
ObjectInputStreamois=new ObjectInputStream(fis);
Dog d2=(Dog)ois.readObject();
System.out.println(d2.i+"........"+d2.j);
}
}
Output:
10........20
10........20
Even though Dog class does not implementsSerializable interface explicitly but we can
Serialize Dog object because its parent class animal already implements Serializable
interface.
At the time of Account object serialization JVM will check is there any writeObject()
method in Account class or not. If it is not available then JVM is responsible to perform
nd nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
217 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
218 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Example 8:
import java.io.*;
class Animal
{
int i=10;
Animal(){
System.out.println("Animal constructor called");
}
}
class Dog extends Animal implements Serializable
{
int j=20;
Dog(){
System.out.println("Dog constructor called");
}
}
classSerializableWRTInheritance
{
nd nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
219 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
220 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR JAVA Means DURGA SIR
At the time of deserialization Jvm will create a seperate new object by executing public
no-arg constructor on that object JVM will call readExternal() method.
Externalization : ( 1.1 v )
1. In default serialization every thing takes care by JVM and programmer doesn't
have any control.
2. In serialization total object will be saved always and it is not possible to save part
of the object , which creates performence problems at certain point.
3. To overcome these problems we should go for externalization where every thing
takes care by programmer and JVM doesn't have any control.
4. The main advantage of externalization over serialization is we can save either
total object or part of the object based on our requirement.
5. To provide Externalizable ability for any object compulsory the corresponding
class should implements externalizable interface. Example :
6. Externalizable interface is child interface of serializable interface.
import java.io.*;
public void readExternal(ObjectInput in) throws IOException , public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(s);
ClassNotFoundException out.writeInt(i);
}
This method will be executed automatically at the time of deserialization with in this public void readExternal(ObjectInput in) throws IOException ,
method , we have to write code to save read required variable from file and assign to the ClassNotFoundException {
current object s=(String)in.readObject();
i= in.readInt();
}
nd nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
221 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
222 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
}
public class Externalizable1 {
JVM and programmer doesn't doesn't have any control.
public static void main(String[] args)throws Exception { have any control
ExternalDemo t1=new ExternalDemo("ashok", 10, 20); Here total object will be saved
FileOutputStreamfos=new FileOutputStream("abc.ser"); Here based on our requirement we can save either total
ObjectOutputStreamoos=new ObjectOutputStream(fos); always and it is not possible to
object or part of the object.
oos.writeObject(t1); save part of the object.
Serialization is the best choice
FileInputStreamfis=new FileInputStream("abc.ser"); Externalization is the best choice if we want to save part
ObjectInputStreamois=new ObjectInputStream(fis); if we want to save total object
of the object.
ExternalDemo t2=(ExternalDemo)ois.readObject(); to the file.
System.out.println(t2.s+"-------"+t2.i+"--------"+t2.j);
relatively performence is low relatively performence is high
}
} Externalizable interface contains 2 methods :
Serializable interface doesn't
1.writeExternal()
contain any method , and it is
2. readExternal()
output : marker interface.
public no-arg constructor It is not a marker interface.
ashok -------- 10 ------ 0 Serializable class not required Externalizable class should compulsory contains public
to contains public no-arg no-arg constructor otherwise we will get
constructor. RuntimeException saying "InvalidClassException"
Diagram :
transient keyword play role in transient keyword don't play any role in
serialization Externalization
1. If the class implements Externalizable interface then only part of the object will
be saved in the case output is
2. public no-arg constructor
3. ashok ---- 10 ----- 0
4. If the class implements Serializable interface then the output is ashok --- 10 --- 20
5. In externalization transient keyword won't play any role , hence transient
keyword not required.
serialVersionUID :
To perform Serialization & Deserialization internally JVM will use a unique identifier ,
Difference between Serialization &Externalization : which is nothing but serialVersionUID .
Serialization Externalization
It is meant for default At the time of serialization JVM will save serialVersionUID with object.
It is meant for Customized Serialization
Serialization
Here every thing takes care by Here every thing takes care by programmer and JVM
nd nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
223 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
224 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR JAVA Means DURGA SIR
At the time of Deserialization JVM will compare serialVersionUID and if it is matched We if configure our own serialVersionUID both sender and receiver not required to
then only object will be Deserialized otherwise we will get RuntimeException saying maintain the same JVM versions.
"InvalidClassException". Note : some IDE's generate explicit serialVersionUID.
1. After Serializing object if we change the .class file then we can't perform
deserialization because of mismatch in serialVersionUID of local class and
serialized object in this case at the time of Deserialization we will get
RuntimeException saying in "InvalidClassException".
2. Both sender and receiver should use the same version of JVM if there any
incompatability in JVM versions then receive anable to deserializable because of
different serialVersionUID , in this case receiver will get RuntimeException
saying "InvalidClassException" .
3. To generate serialVersionUID internally JVM will use complexAlgorithm which
may create performence problems.
Example :
In the above program after serialization even though if we perform any change to
Dog.classfile , we can deserialize object.
nd nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
225 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
226 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com