Serialization PDF
Serialization PDF
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
201 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
202 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR
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
form to either network supported form (or) file supported form.
3. By using FileOutputStream and ObjectOutputStream classes we can achieve
serialization process.
4. Ex: big ballon
Diagram:
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
203 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR
De-Serialization:
1. The process of reading state of an object from a file is called DeSerialization
2. but strictly speaking it is the process of converting an object from file supported
form (or) network supported form to java supported form.
3. By using FileInputStream and ObjectInputStream classes we can achieve
DeSerialization.
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
204 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR
Diagram:
Example 1:
import java.io.*;
class Dog implements Serializable
{
int i=10;
int j=20;
}
classSerializableDemo
{
public static void main(String args[])throws Exception{
Dog d1=new Dog();
System.out.println("Serialization started");
FileOutputStreamfos=new FileOutputStream("abc.ser");
ObjectOutputStreamoos=new ObjectOutputStream(fos);
oos.writeObject(d1);
System.out.println("Serialization ended");
System.out.println("Deserialization started");
FileInputStreamfis=new FileInputStream("abc.ser");
ObjectInputStreamois=new ObjectInputStream(fis);
Dog d2=(Dog)ois.readObject();
System.out.println("Deserialization ended");
System.out.println(d2.i+"................"+d2.j);
}
}
Output:
Serialization started
Serialization ended
Deserialization started
Deserialization ended
10................20
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
205 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR
Diagram:
Note:
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
206 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR
Example2:
import java.io.*;
class Dog implements Serializable
{
int i=10;
int j=20;
}
class Cat implements Serializable
{
int i=30;
int j=40;
}
classSerializableDemo
{
public static void main(String args[])throws Exception{
Dog d1=new Dog();
Cat c1=new Cat();
System.out.println("Serialization started");
FileOutputStreamfos=new FileOutputStream("abc.ser");
ObjectOutputStreamoos=new ObjectOutputStream(fos);
oos.writeObject(d1);
oos.writeObject(c1);
System.out.println("Serialization ended");
System.out.println("Deserialization started");
FileInputStreamfis=new FileInputStream("abc.ser");
ObjectInputStreamois=new ObjectInputStream(fis);
Dog d2=(Dog)ois.readObject();
Cat c2=(Cat)ois.readObject();
System.out.println("Deserialization ended");
System.out.println(d2.i+"................"+d2.j);
System.out.println(c2.i+"................"+c2.j);
}
}
Output:
Serialization started
Serialization ended
Deserialization started
Deserialization ended
10................20
30................40
Transient keyword:
1. transient is the modifier applicable only for variables.
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
207 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR
Static VsTransient :
1. static variable is not part of object state hence they won't participate in
serialization because of this declaring a static variable as transient there is no
use.
Transient Vs Final:
1. final variables will be participated into serialization directly by their values.
Hence declaring a final variable as transient there is no use.
//the compiler assign the value to final variable
Example 3:
import java.io.*;
class Dog implements Serializable
{
static transient int i=10;
final transient int j=20;
}
classSerializableDemo
{
public static void main(String args[])throws Exception{
Dog d1=new Dog();
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);
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
208 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR
}
}
Output:
10................20
Diagram:
Table:
declaration output
int i=10;
10................20
int j=20;
transient int i=10;
0................20
int j=20;
transient int i=10;
0................20
transient static int j=20;
transient final int i=10;
10................0
transient int j=20;
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
209 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR
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);
Dog d2=(Dog)ois.readObject();
Cat c2=(Cat)ois.readObject();
Rat r2=(Rat)ois.readObject();
Example :
FileInputStreamfis=new FileInputStream("abc.ser");
ObjectInputStreamois=new ObjectInputStream(fis);
Object o=ois.readObject( );
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
210 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR
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
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
211 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR
{
public static void main(String args[])throws Exception{
Dog d1=new Dog();
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.c.r.j);
}
}
Output:
20
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
212 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR
Diagram:
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
213 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR
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
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
214 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR
In the above example before serialization Account object can provide proper
username and password. But after Deserialization Account object can provide
only username bur not password. This is due to declaring password as transient.
Hence doing default serialization there may be a chance of loss of information
due to transient keyword.
We can recover this loss of information by using customized serialization.
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
215 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR
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);
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.........kajal
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
216 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR
Diagram:
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
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
217 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
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;
}
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.
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
218 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR
Case 2:
1. Even though parent class does not implementsSerializable we can serialize child
object if child class implements Serializable interface.
2. At the time of serialization JVM ignores the values of instance variables which
are coming from non Serializable parent then instead of original value JVM
saves default values for those variables to the file.
3. At the time of Deserialization JVM checks whether any parent class is non
Serializable or not. If any parent class is nonSerializable JVM creates a separate
object for every non Serializable parent and shares its instance variables to the
current object.
4. To create an object for non-serializable parent JVM always calls no arg
constructor(default constructor) of that non Serializable parent hence every non
Serializable parent should compulsory contain no arg constructor otherwise we
will get runtime exception "InvalidClassException" .
5. If non-serializable parent is abstract class then just instance control flow will be
performed and share it's instance variable to the current object.
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
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
219 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR
Diagram:
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
220 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR
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.
6. Externalizable interface is child interface of serializable interface.
This method will be executed automaticcay at the time of Serialization with in this
method , we have to write code to save required variables to the file .
This method will be executed automatically at the time of deserialization with in this
method , we have to write code to save read required variable from file and assign to the
current object
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
221 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
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.
Example :
import java.io.*;
publicExternalDemo() {
System.out.println("public no-arg constructor");
}
publicExternalDemo(String s , int i, int j) {
this.s=s ;
this.i=i ;
this.j=j ;
}
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
222 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR
}
public class Externalizable1 {
public static void main(String[] args)throws Exception {
ExternalDemo t1=new ExternalDemo("ashok", 10, 20);
FileOutputStreamfos=new FileOutputStream("abc.ser");
ObjectOutputStreamoos=new ObjectOutputStream(fos);
oos.writeObject(t1);
FileInputStreamfis=new FileInputStream("abc.ser");
ObjectInputStreamois=new ObjectInputStream(fis);
ExternalDemo t2=(ExternalDemo)ois.readObject();
System.out.println(t2.s+"-------"+t2.i+"--------"+t2.j);
}
}
output :
public no-arg constructor
ashok -------- 10 ------ 0
Diagram :
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.
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
223 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR
serialVersionUID :
To perform Serialization & Deserialization internally JVM will use a unique identifier ,
which is nothing but serialVersionUID .
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
224 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR
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
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
225 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR
We if configure our own serialVersionUID both sender and receiver not required to
maintain the same JVM versions.
Note : some IDE's generate explicit serialVersionUID.
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
226 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
227 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com