Serialization: -
- The process of saving state of an object to a file is called serialization. But strictly speaking serialization is the process of converting an object from java supported form to either file supported form or network supported form.
- By using FileOutputStream and ObjectOutputStream classes we can achieve serialization.
De-serialization: -
- The process of reading state of an object from a file is called de-serialization. But strictly it is process of converting an object from file supported form or network supported form to java supported form.
- By using FileInputStream and ObjectInputStream classes we will achieve de-serialization.
Example:
Class to be serialized:
package com.javamonkeys.serialization; import java.io.Serializable; public class User implements Serializable { private String userName; private String passWord; public User(String userName, String passWord) { this.userName = userName; this.passWord = passWord; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; } }Main Class:
package com.javamonkeys.serialization; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class SerializableExample { public static void main(String[] args)throws IOException, ClassNotFoundException { String userName="javamonkey"; String passWord="password"; User user=new User(userName, passWord); FileOutputStream fiOutputStream = new FileOutputStream("D:\\serializablefile.ser"); ObjectOutputStream oStream = new ObjectOutputStream(fiOutputStream); oStream.writeObject(user); FileInputStream fileInputStream = new FileInputStream("D:\\serializablefile.ser"); ObjectInputStream oInputStream = new ObjectInputStream(fileInputStream); User usr = (User)oInputStream.readObject(); System.out.println("User name:"+usr.getUserName()); System.out.println("Password:"+usr.getPassWord()); } }
- User is class which is to be serialized. In the SerializableExample class lines 14, 15 and 16 represents the serialization process and lines 18, 19 and 20 represents the de-serialization process. That is in the lines 14, 15 and 16 we are saving the state of a User object to the file called serializablefile.ser. Here .ser extension represents the Serializable file. If we open the file we can’t understand what is there in the file because it is file supported form. After de-serializing we are printing the results that can be understandable. That means after de-serializing object is converted to java understandable form.
- We can perform serialization process only on Serializable objects.
- An object is said to be Serializable if and only if the corresponding class implements Serializable interface.
- Serializable interface present in java.io package and doesn’t have any method. It is a marker interface.
- If we try to serialize a non Serializable object (class that not implemented Serializable), then we will get NotSerializableException, it is a Runtime Exception.
transient:-
- At the time of Serialization if we don’t want to send the value of any particular variable to meet security constraints, we have to declare such type of variable as transient.
- transient means not to Serialize.
- At the time of serialization process JVM sends the default values instead of original values if we declare that particular variable as transient.
- Static variables are not part object, these are the part of class hence, and static variables won’t participate in the serialization process. Even though we declare static variables as transient there will be no difference.
- Final variables will not change to default values even if we make them as transient.
- The below table illustrates the different possible combinations of inputs and outputs with the above example (change the types of variables in the User class not in the Main class).
Declaration
|
Output
|
String userName=”monkey”
String passWord=”password”
|
userName: monkey
password: password
|
transient String
userName=”monkey”
String
passWord=”password”
|
userName: null
password: password
|
String
userName=”monkey”
transient static String
passWord = “password”
|
userName: monkey
password: password
|
transient final
String userName=”monkey”
transient String
passWord=”password”
|
userName: monkey
password: null
|
transient static String userName=”monkey”
transient final String passWord=”password”
|
userName: monkey
password: password
|
No comments:
Post a Comment