Introduction to serialization
We know that object is a real time entity, that means object will be created during runtime of application/program, object can be stored in a JVM memory components until ending of program execution if required, or can be deleted in the middle of program execution with the help of Garbage Collector. But if we want store object data in a file or in a database like normal data ( example: age, name etc..), yes that is possible with serialization concept in java.
Serialization is the process to convert object into sequence of bytes, which holds information object such as type of object and data associated with object.
What is deserialization?
Deserialization is the process to convert byte stream into an object.Key points related to serialization and deserialization
- Serialized object data can be deserialized outside of JVM too, that means serialized object data is independent from platform.
- Serialized data can be stored in a file or data base ( as BLOB type of data )
- We can deserialize byte stream which stored in a file and database
- We can trnasfer serialized object through the network to remote business component , for example sending chat message to chat server
- Serializable class must implements java.io.Serialize or java.io.Externalizable interface
- All variables will be serialized automatically
- If you do not want to serialize any variable, that variable must me declared as transient variable
- All related other objects are also be serialized apart from current object
Compatible changes allowing in serialization
For better understanding of what is Compatible changes means , let us consider a situation, that we have a class Student.java, this class is serialized and stored byte stream in a file A, after some time Student.java class is altered, then we want to deserialize byte stream from a file A into Student.java ( new one, which is altered from old one ), then in this case an object representing in a file A is different than the object of new Student.java class, then what changes will allow in a Student.java class will tell us by allowing compatible changes.In the following example first case deals with serialization and deserialization on the same version of a Student.java class, but in the second part of the diagram you can see that newer class of Student.java ( which are coloured with red color )
Compatible changes with versions of class in serialization |
Compatible Changes means: what changes in new version of class will be allowed to compatible with older version class
Changing field and method are compatible changes
Detailed description about compatible changes
- Changing field access specifier will be allowed in a newer version of Class
- Adding new field will be allowed in a newer version of class
- Changing static to non static allowed
- Changing transient to non-transient allowed
- We can change the access modifiers for constructors and methods of the class. For instance a previously private method can now be made public, an instance method can be changed to static, etc.
- We cannot change the default signatures for readObject() and writeObject() if we are implementing custom serialization.
The serialization process looks at only instance data, and not the methods of a class.
Incompatible changes with in serialization
- We cannot change non-static to static and non-transient to transient
- We cannot delete the field
- We cannot change field type in a class
- We cannot change package and class name
- We cannot change the hierarchy of class
- We cannot unimplement serializable interface
ObjectOutputStream for serializing object
We can use the class ObjectOutputStream to create byte stream of object. We have wrtieObject() method in ObjectOutputStream, this method will serialize an object and store the data into a file.Steps to serialize and store byte stream object into a file
- First create file output stream object
- Create ObjectOutputStream object by passing file output stream object
- Invoke writeObject() on ObjectOutputStream object by passing which object to be serialized and stored
public class Account implements Serializable { private static final long serialVersionUID = 1L; int acountNo; String bankName; public Account(int acountNo, String bankName) { super(); this.acountNo = acountNo; this.bankName = bankName; } public int getAcountNo() { return acountNo; } public void setAcountNo(int acountNo) { this.acountNo = acountNo; } public String getBankName() { return bankName; } public void setBankName(String bankName) { this.bankName = bankName; } }
Account account= new Account(123, "World Bank"); File as= new File("bank.txt"); try { FileOutputStream fout= new FileOutputStream(as); ObjectOutputStream oos= new ObjectOutputStream(fout); oos.writeObject(account); oos.close(); fout.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }In the above code after executing we can get a file named as bank.txt with object stored in it
ObjectInputStream for deserializing an object
Now we will see how to deserialize an object stored in a file ( from the above example it is bank.txt ), we can use ObjectInputStream and its readObject() method to read and deserialize an objectSteps to deserialize an object
- First create a File object to the file where we stored object ( in our example bank.txt )
- Create FileInputStream object for the file
- Create ObjectInputStream for the above FileInputStream object
- Invoke readObject() of ObjectInputStream which returns Object class object
File as = new File("bank.txt"); FileInputStream fis = new FileInputStream(as); ObjectInputStream ois = new ObjectInputStream(fis); Object object = ois.readObject(); System.out.println(object.getClass().getCanonicalName());
Post a Comment
Post a Comment