Tuesday, 17 January 2017

Difference between Serializable and Externalizable in Java?

Instead of implementing Serializable interface,  a developer can also implement Externizable interface which thus extends Serializable, developer is responsible for implementing writeExternal() and readExternal() methods to have control over reading and writing serialized objects.By implementating java.io.Serializable, you get "automatic" serialization capability for objects of your class. No need to implement any other logic, it'll just work.

 The Java runtime will use reflection to figure out how to marshal and unmarshal your objects.In earlier version of Java, reflection was very slow, and so serializaing large object graphs (e.g. in client-server RMI applications) was a bit of a performance problem. To handle this situation, the java.io.Externalizable interface was provided, which is like java.io.Serializable but with custom-written mechanisms to perform the marshalling and unmarshalling functions (you need to implement readExternal and writeExternal methods on your class). This gives you the means to get around the reflection performance bottleneck.

No comments:

Post a Comment