Re: advantage of externalizable
Posted By:
vijay_chaudhary
Posted On:
Tuesday, May 23, 2006 01:06 AM
Externalizable is used when we have to serialize a part of code bcoz when we use Serializable then whole class which is implementing Serializable interface will become serialized means data is converted to bitblob.
and suppose we have to compress data before serializing then we need Externalizable .
when we are implementing Externalizable then we have to override two methods: readExternal() , writeExternal();
code:
public class ExternalizableDemo implements Externalizable {
public int lowBid = 0;
public String highBid = "";
public void writeExternal(ObjectOutput out)throws IOException{
out.writeInt(lowBid);
out.writeObject(highBid);
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException{
lowBid = in.readInt();
highBid = (String)in.readObject();
}
public String toString(){
return " "+lowBid+"high: "+highBid;
}
}