🚀 Enjoying Java Tutorials?
Subscribe to Ram N Java for clean, beginner-friendly core Java tutorials and interview prep!
🔔 Subscribe to Ram N JavaIntroduction
When working with object persistence and transmission in Java, choosing the right mechanism between Serializable and Externalizable makes a significant difference in both execution performance and data control. While both interfaces allow converting Java objects into byte streams, their operational models are distinctly different.
Key Differences Explained
1. Interface Type and Signatures
Serializable: Belongs to java.io and acts as a marker interface. It has no methods or fields. Implementing it gives the JVM permission to handle object serialization automatically using reflection.
Externalizable: A child interface of Serializable containing two mandatory callback methods:
public void writeExternal(ObjectOutput out) throws IOExceptionpublic void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
2. Performance & Control
Serializable: Easy to implement, but default serialization incurs reflection overhead and writes extra class metadata into the stream.
Externalizable: Gives complete control to the developer. Because you choose precisely which fields to serialize, it can achieve significantly higher performance and compact byte payloads.
3. Constructor Requirements
Serializable: During deserialization, no constructor is executed for the serialized class itself.
Externalizable: Deserialization requires a public no-argument constructor to instantiate the object before invoking readExternal().
4. Impact of the transient Modifier
Serializable: Fields marked with transient are ignored by the default serialization mechanism.
Externalizable: The transient keyword has no impact because all field persistence logic is manually written inside the method bodies.
Quick Reference Summary
| Criteria | Serializable | Externalizable |
|---|---|---|
| Control | Automatic (JVM default) | Manual (Developer custom) |
| Method Count | 0 (Marker) | 2 (writeExternal / readExternal) |
| No-arg Constructor | Not required | Mandatory (Must be public) |
| Performance | Standard / Slower (Reflection) | Fast and customizable |
Related Tutorials from Ram N Java
Click here to watch in Youtube :
https://www.youtube.com/watch?v=IxyUijSQ_zk&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge
![]() |
| Java Tutorial : Java IO (Java serializable vs externalizable | serialization vs externalization -V5) |

No comments:
Post a Comment