After knowing what is serialization, let us know something more. To support serialization, java.io package comes with interface Serializable and two classes ObjectOutputStream and ObjectInputStream.
1. class java.io.ObjectOutputStream
Following is the class signature
public class ObjectOutputStream extends OutputStream implements ObjectOutput, ObjectStreamConstants
The methods of ObjectOutputStream are capable to save the state of primitive data types to an OutputStream. Only the serialized objects are allowed to write.
Some write methods of ObjectOutputStream
- public void writeBoolean(boolean) throws java.io.IOException;
- public void writeByte(int) throws java.io.IOException;
- public void writeShort(int) throws java.io.IOException;
- public void writeInt(int) throws java.io.IOException;
- public void writeLong(long) throws java.io.IOException;
- public void writeFloat(float) throws java.io.IOException;
- public void writeDouble(double) throws java.io.IOException;
- public void writeBytes(java.lang.String) throws java.io.IOException;
- public void writeUTF(java.lang.String) throws java.io.IOException;
- public final void writeObject(java.lang.Object) throws java.io.IOException;
2. class java.io.ObjectInputStream
Following is the class signature
public class ObjectInputStream extends InputStream implements ObjectInput, ObjectStreamConstants
An ObjectInputStream is capable to deserialize primitive data and objects written earlier with ObjectOutputStream. Only serialized object (classes implementing java.io.Serializable or java.io.Externalizable) are allowed to read. Reading the object and restoring the state is known as deserialization (reverse of Serialization) and can done by the reading methods of ObjectInputStream.
An object written (to save the state) with writeObject() method of ObjectOutputStream can be read (otherway, restore the state) with readObject() method of ObjectInputStream.
Some reading methods of ObjectInputStream
- public boolean readBoolean() throws java.io.IOException;
- public byte readByte() throws java.io.IOException;
- public char readChar() throws java.io.IOException;
- public short readShort() throws java.io.IOException;
- public int readInt() throws java.io.IOException;
- public long readLong() throws java.io.IOException;
- public float readFloat() throws java.io.IOException;
- public double readDouble() throws java.io.IOException;
- public java.lang.String readLine() throws java.io.IOException;
- public java.lang.String readUTF() throws java.io.IOException;
- public final java.lang.Object readObject() throws java.io.IOException, java.lang.ClassNotFoundException;
3. interface java.io.Serializable (introduced with JDK 1.1)
Following is the class signature
Compiled from “Serializable.java”
public interface java.io.Serializable {
}
Observe no methods in the interface.
The Serializable interface is very peculiar, in that, it does not contain any methods. The interface wihtout any methods is known as Marker interface or Tag interface or Empty interface. Some people may get a doubt of the usage of marker interface without implementing any methods in the subclass. A marker interface gives special instructions to the JVM to process the object suitable for a special task.
The subclasses of a serialized class are also serialized implicitly. An example can be taken from Java API.,
class Number implements Serializable
class Integer extends Number
Because the super class of Integer, the Number is serialized, the Integer objects are also serialized.
For example, implementation of Serializable interface gives special instructions to the JVM to process the object suitable to write to a file.
What we can do, in Java, with a serialized object?
1. An object can be written to a file.
Even if you try to write a non-serialized object to a file, the JVM does not allow to write and throws "NotSerializableException".
2. To send the object across a distributed network as in RMI (Remote Method Invocation).
In RMI, a distributed technology of Java, objects speak together (known as object-to-object communication). An object on a Java client (from one country) can communicate with another Java object (situated in a different country). For example, an ATM center from your locality can communicate with a Bank server present in a different country. In RMI, objects travel across network. These objects also should be serialized because data persistence and state of the object should be preserved.
Following list gives marker interfaces, Java comes with.
There are total 7 empty interfaces in Java API.
Maker interface name | Functionality |
---|---|
1. java.io.Serializable | the object is serialized |
2. java.lang.Cloneable | the object is cloned |
3. javax.servlet.SingleThreadModel | only one object of the servlet for one request is created |
4. javax.ejb.EnterpriseBean | the object can participate in transactions |
5. java.util.RandomAccess | the elements of the data structure can be randomly retrieved fast (introduced in jdk1.5) |
6. java.rmi.Remote | the object can invoke the methods on a remote machine |
7. java.util.EventListener | All event listeners extend this interface |
Next program gives an idea of how to use Serializable, ObjectOutputStream and ObjectInputStream.
sir , why in hibernate we dont serialize objects , even there we are writing them to database .
thanks
You are saving object in Hibernate. When saved, object is not written to database, but data is entered in columns by writing query itself.