What is the use of the java Externalization when custom serialization is possible by overriding the writeObject() and readObject()?

397 views Asked by At

I am learning java serialization and i have a doubt, if one can customize the default serialization process by overriding the writeObject() and readObject() methods in class then what is the use of the Externalizable interface? in which scenario it is needed?

1

There are 1 answers

0
Ori Marko On BEST ANSWER

It's used for custom serialization, see Guide to the Externalizable

Main usage:

change the JVM’s default serialization behavior.

Use case:

If we need to serialize the entire object, the Serializable interface is a better fit. On the other hand, for custom serialization, we can control the process using Externalizable.

Possible performance advantage:

The java.io.Serializable interface uses reflection and metadata which causes relatively slow performance. By comparison, the Externalizable interface gives you full control over the serialization process.