Posts

Showing posts with the label Serialization in Java

Serializable vs Externizable

Serialization vs Externalization Serializable Externizable Serializable is a marker interface (an interface with no methods) Unlike Serializable, Externizable is a standard interface with two methods defined to be implemented by the implementing class. Serailization is a recursive process, all non-transient variables and super classes in the object hierarchy will be serialized causing an unnecessary overhead User defines what should be serialized and what should not. Hence it is more optimized. Should be preferred for "Fat Objects"  Serialization uses reflection mechanism for marshalling and un marshalling the objects.  Marshalling/Unmarshalling process is user defined. During de-serialization no constructor is called, hence initialization done in constructor will be skipped.  During de-serialization default constructor is invoked A default construtor defini...

The Externalizable Interface

Before you start exploring Externalization in Java, I would recommend you to gather some knowledge of Serialization in Java because Externalizable is an alternative to Serializable interface. Externalizable interface allows you to customize how serialization is done. By implementing Externalizable you are controlling what gets serialized and what does not get serialized.  Externalizable interface  extends Serializable interface and defines two methods to be overridden by the implementing class.  public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException public void writeExternal(ObjectOutput out) throws IOException Java class implementing Externalizable interface package learn.java.serialization; import java.io.Externalizable; import   java.io.IOException; import   java.io.ObjectInput; import   java.io.ObjectOutput; import   java.util.Date; public class ExternalizableObject implements Exte...

Serialization in Java

Serialization is the process of converting an object's state into byte sequence ( to persist it on to a file ), and to rebuild ( deserialize ) those bytes into an object. To make an object serializable, the object should implement java.io.Serializable interface. Lets take an example to understand Serialization in Java. Firstly lets define a serializable object ############################################################### package learn.java.serialization; import java.io.Serializable; public class SerializableObject implements Serializable { private String message; public SerializableObject(){ } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } ############################################################### As you must have noticed the only special thing that we have done  to the class is implement Serializable. The java.io.Serializable ...