0% found this document useful (0 votes)
9 views

3.object Serialization

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

3.object Serialization

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Object

Serialization

1 © 2012 WIPRO LTD | WWW.WIPRO.COM


Agenda

Object Serialization

2 © 2012 WIPRO LTD | WWW.WIPRO.COM


Objectives
At the end of this module, you will be able to:

– Understand Object Serialization

3 © 2012 WIPRO LTD | WWW.WIPRO.COM


Object Serialization

4 © 2012 WIPRO LTD | WWW.WIPRO.COM


Serialization

• Object serialization is the process of saving an


object's state to a sequence of bytes (on disk), as
well as the process of rebuilding those bytes into a
live object at some future time

• The Java Serialization API provides a standard


mechanism to handle object serialization

• You can only serialize the objects of a class that


implements Serializable interface

5 © 2012 WIPRO LTD | WWW.WIPRO.COM


Serialization

• After a serialized object is written to a file, it can be read from t


he file and
deserialized (that is we can recreate the object in memory)

• The process of Serialization and DeSerialization is JVM indepe


ndent. That is, an object can be serialized on one platform and
deserialized on an entirely different platform.

• Classes ObjectInputStream and ObjectOutputStream are us


ed for serialization & deserialization.

6 © 2012 WIPRO LTD | WWW.WIPRO.COM


Serializing Objects
• How to Write to an ObjectOutputStream
FileOutputStream out = new FileOutputStream("theTime");
ObjectOutputStream s = new ObjectOutputStream(out);
s.writeObject("Today");
s.writeObject(new Date());
s.flush();

• How to Read from an ObjectOutputStream


FileInputStream in = new FileInputStream("theTime");
ObjectInputStream s = new ObjectInputStream(in);
String today = (String)s.readObject();
Date date = (Date)s.readObject();

7 © 2012 WIPRO LTD | WWW.WIPRO.COM


Object Serialization
package m10.io;
import java.io.*;

public class MyClass implements Serializable {


String s;
int i;
double d;
public MyClass(String s, int i, double d) {
this.s = s;
this.i = i;
this.d = d;
}
public String toString() {
return “s=“ + s + “; i=“ + i + “; d=“ + d;
}
}

8 © 2012 WIPRO LTD | WWW.WIPRO.COM


Object Serialization (Contd.).
public class SerializationDemo {

public static void main(String args[]) {


try {
MyClass object1 = new MyClass(“Hello”, -7, 2.7e10);
System.out.println(“object1; “ + object1);
FileOutputStream fos = new FileOutputStream(“serial”);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(object1);
oos.flush();
oos.close();
}
catch(Exception e) {
System.out.println(“Exception during serialization:“+ e);
System.exit(0);
}

9 © 2012 WIPRO LTD | WWW.WIPRO.COM


Object Serialization (Contd.).
// Object Deserialization
try {
MyClass object2;
FileInputStream fis = new FileInputStream(“serial”);
ObjectInputStream ois = new ObjectInputSream(fis);
object2 = (MyClass)ois.readObject();
ois.close();
System.out.println(“object2: “ + object2);
}
catch(Exception e) {
System.out.println(“Exception during deserialization: “ + e);
System.exit(0);
}
}
}

10 © 2012 WIPRO LTD | WWW.WIPRO.COM


The keyword : transient
transient keyword is used in Object Serialization.

By default, when you serialize an object, all its fields


are serialized except for static variables. When you
construct this object back from its persistent state,
you will get the values of all the fields that are
serialized(except static variables).

If you do not want to store the value of a particular


non-static field, then you can declare this field as
transient.

This keyword is used only with a variable declaration.

11 © 2012 WIPRO LTD | WWW.WIPRO.COM


The keyword : transient

• Transient keyword provides us with the ability to control the ser


ialization process
and gives us the flexibility to exclude some of object properties
from serialization process.

• Sometimes, it does make sense not to serialize certain attribut


es of an object. For e.g. If you are developing an application for
Weather forecasting and you
have created objects that store current weather conditions, the
n storing current
temperature does not make much sense, since temperature ke
eps
fluctuating and you may not require the temp data at a later dat
e when you de-serialize this object.

12 © 2012 WIPRO LTD | WWW.WIPRO.COM


Demo : transient

import java.io.*;
class Xyz implements Serializable {
double d1; Try this demo first by declaring the
transient double d2; variable d2 as non-transient(delete the
static double d3; key word transient). Try again by
void m1() { declaring the variable d2
as transient and observe the
difference

System.out.println("The value of d1 is :" +d1);


System.out.println("The value of d2 is :" +d2);
System.out.println("The value of d3 is :" +d3);
}
}
13 © 2012 WIPRO LTD | WWW.WIPRO.COM
Demo : transient

class TransientExample1 {
public static void main(String [] args) throws IOException {
Xyz x = new Xyz();
x.d1=10.3;
x.d2=20.5;
x.d3=99.99;
x.m1();
FileOutputStream fx = new FileOutputStream("A1.xyz");
ObjectOutputStream ox = new ObjectOutputStream(fx);
ox.writeObject(x);
ox.flush();
}
}

14 © 2012 WIPRO LTD | WWW.WIPRO.COM


Demo : transient

import java.io.*;
class TransientExample2 {
public static void main(String [] args) {
try {
FileInputStream fx = new FileInputStream("A1.xyz");
ObjectInputStream ox = new ObjectInputStream(fx);
Xyz x = (Xyz) ox.readObject();
x.m1();
}
catch(Exception e) {
System.out.println(e);
}
}
}

15 © 2012 WIPRO LTD | WWW.WIPRO.COM


Demo : transient
• Scenario 1 : When d2 is not transient !

• When you compile all the three source files viz. Xyz.java,
TransientExample1.java and TransientExample2.java and
execute first
TransientExample1 and then TransientExample2, you will
get the following output (from executing TransientExampl
e2):
• The value of d1 is :10.3
• The value of d2 is :20.5
• The value of d3 is :0.0

• In the above result, d3 is not serialized since d3 is declare


d as static.

16 © 2012 WIPRO LTD | WWW.WIPRO.COM


Demo : transient

• Scenario 1 : When d2 is transient !

• After declaring d2 as transient, when you compile Xyz.java and then


execute first TransientExample1 and then TransientExample2,
you will get the following output :
• The value of d1 is :10.3
• The value of d2 is :0.0
• The value of d3 is :0.0

• In the above result, d2 is not serialized since it is declare as transient.

17 © 2012 WIPRO LTD | WWW.WIPRO.COM


Assignment

18 © 2012 WIPRO LTD | WWW.WIPRO.COM


Thank You

19 © 2012 WIPRO LTD | WWW.WIPRO.COM

You might also like