Serialization is a process where a present state of object will be saved in a bytes stream. The byte stream is neutral so objects created in other platform can be deserialized in other platforms. De-serialization is exactly the reverse option of serialization. Serialization process are mainly used during EJB, JMS, JPA and RMI. java.io.serializable interface is implemented in all string class and wrapper class.
Read: Java access modifiers
Advantages of Serialization in Java:
- Serialization is mainly used to marshaling the travel objects network.
- The process of serialization is easy to use and can be easily customized.
- The serialized team can be encrypted, automated and compressed to secure the need of Java computing.
- The gradual evolution of schema in application object are flexible and coherent in serialized classes.
- Java serialization can be used as tool to exchange objects between java and C++ libraries.
- Critical technologies like RMI, EJB and JavaBeans rely extremely on Java serialization.
Serialization Disadvantages in Java:
- Serialization in Java cannot be used with objects which are very large in size. The memory requirements for large sized objects are high because the input and output streams are either reset or closed. The garbage of these very large objects will be delayed because of serialization.
- Control in object access will not provide control in serializable interface but this issue can be resolved by establishing complex externalizable interface.
- Serialization is not suitable for application which requires concurrent access without API’s because it won’t provide traction control mechanisms.
java.io.serializable Interface
Serialization marker has no body and it is used to mark java classes with certain capability. It is implemented in a class whose object you want to be prevailed.
Check: How to Declare and Extended an Interface using Java
ObjectOutputStream
The coding of primitive data types and java objects is done using objectoutputstream. Objects which support java.io.serializable is written using streams.
The General Syntax to write Java Serialization is:
public class SomeNewClass implements java.io.Serializable
{
// this class is serializable
…
}
Java Serialization can be explained using the following program:
import java.io.*;
class school {
public static void main(String args[ ])throws Exception
{
Stud stu1 =new Stud(211,”Ajith”);
FileOutputStream f1=new FileOutputStream(“f1.txt”);
ObjectOutputStream out=new ObjectOutputStream(fout);
out.writeObject(stu1);
out.flush();
System.out.println(“The success”);
}
}
The output of this program is “The Success”
Deserialization
Deserialization is the reverse process of serialization; an object is reconstructed from serialized state during the deserialized state.
ObjectInputStream
Primitive data is written using an ObjectOutputStream while ObjectInputStream is used to deserialized object. Public objectinputstream will thrown an exception.
- Many: Java interview Q&A available here
Example for Java Deserialization
import java.io.*;
class Persist{
public static void main(String args[])throws Exception
{
Stud stu1 =new Stud(211,”Ajith”);
FileOutputStream f1=new FileOutputStream(“f.txt”);
ObjectOutputStream o1=new ObjectOutputStream(fout);
o1.writeObject(stu1);
out.flush();
System.out.println(“The success”);
}
}
The output for this program is 211Vijay.
Transient Variables and Serializable classes
If you want to avoid a particular object from serializing then you can declare that object as transient. The transient variables will not be part of the persistent state of objects which are serialized.
The syntax given below is an example of transient variable in serializable class
public class SomeNewClass implements java.io.Serializable
{
// this variable will not persist
private transient String password;
…
}
If you serialize an object into file, the associated transient variable will not be the part of the file.