How is Externalizable different from Serializable?

I read that

Externalizable provides us with the writeExternal () and readExternal () methods, which give us the flexibility to control the Java serialization mechanism, rather than relying on the default Java serialization.

But if I implement Serializable and override readObject(), writeObject(), , then doesn't that mean the same as setting up the serialization process?

How is he different?

THANKS.

+4
source share
2 answers

Difference between Externalizable and Serializable

  • Serializable uses the default serialization process. while in the case of an equalized user process, Serialization is used, which is implemented by the application.
  • The JVM returns a call to readExternel () and writeExternal () on the java.io.Externalizalbe interface to restore and write objects to persistence.
  • The Externalizable interface provides complete control over the serialization process for the application.
  • readExternal () and writeExternal () replace any specific implementation of the writeObject and readObject methods.

Although Externalizable provides complete control, it also creates problems for serializing the superuser state and takes care of the default values ​​in the case of a transition variable and static variables in Java. Used correctly, the Externalizable interface can improve the performance of the serialization process.

So go to the Externalizable interface

If you have special requirements for serializing an object. For example, you may have some security-sensitive parts of an object, such as passwords, that you do not want to store or transfer anywhere. Or it may be useless to save a specific object referenced by the main object, because after restoration its value will become useless.

+4
source

White Papers in Bean Perseverance

Implement writeObject when you need to tighten control over what is serialized, when you need to serialize objects that serialization cannot handle by default, or when you need to add data to a serialization stream that is not a member of the object's data. Implement readObject to restore the data stream that you wrote using writeObject.

Use the Externalizable interface if you need full control over Bean serialization (for example, when writing and reading a specific file format). To use the Externalizable interface, you need to implement two methods: readExternal and writeExternal. Classes that implement Externalizable must have a constructor with no arguments.

+2
source

Source: https://habr.com/ru/post/1499211/


All Articles