Serialization
It’s a mechanism by which you can save or transfer the state of an object by converting it to a byte stream.
It’s required to send the state of an object over a network through a socket.
It’s required to send the state of an object over a network through a socket.
Serialization in Java
It can be done by implementing Serialiazable interface.
Note: Serialiazable interface is a marker interface.
Note: Serialiazable interface is a marker interface.
Serializable vs Externalizable
| Serializable | Externalizable |
| Marker interface | Contains methods readExternal() and writeExternal() |
| Inbuilt serialization mechanism | Serialization mechanism can be defined using readExternal() and writeExternal(). |
Serialization performance optimization
Unwanted or non Serializable attributes should be marked as transient.
Save only the state of the object, not the derived attributes.
Don’t serialize objects with default values.
Save only the state of the object, not the derived attributes.
Don’t serialize objects with default values.
Serial Version UID
It represents class version. During serialization it is associated with serialized class and thus it is required at the time of
deserialization for checking of compatibility. If not declared, Java compiler will make one by creating hash code using most of your class attributes and
features.
Transient variables
The transient keyword in Java is used to indicate that a field should not be serialized.
Comments
Post a Comment