Serialization in Java
Serialization means converting any objects into stream of bytes which can be stored files. In java, objects can be serialized and stored into any output stream. Serialized objects can be stored into files or it can be send across a network. An object can only be serialized if its class implements the Serializable interface which is present in the package java.io.*. If the class does not implement the Serializable interface, then its object cannot be serialized. Serialization in java helps in RMI where methods which might receive values do some process in some other machine and may return values to the caller machine. One needs to make sure that the values which are sent or received are Marshalled form.
An example of a class that can be serialized :-
class abc implements java.io.Serializable {
private int x;
abc (int a) {
x=a;
}
}
An object of the above class can be serialized into any output as it implements the Serializable interface.
An example where an object is being serialized.
import.io.*;
class xyz {
public static void main(String args[]) {
ObjectOutput o = new ObjectOutputStream(
new FileOutputStream("file1"));
abc a=new abc();
o.writeObject(a);
}
}
No comments:
Post a Comment