🚀 Enjoying Java Tutorials?
Subscribe to Ram N Java for clean, beginner-friendly core Java tutorials and interview prep!
🔔 Subscribe to Ram N JavaIntroduction
In Java, converting an object into a byte stream can be achieved using two main interfaces from the java.io package: Serializable and Externalizable. While both allow objects to be saved or sent over a network, they work in fundamentally different ways when it comes to JVM automation, performance, and customization.
Key Differences: Serializable vs Externalizable
1. Default vs Manual Serialization
Serializable: Managed entirely by the JVM's default mechanism. It automatically serializes the object's non-transient state without requiring custom code.
Externalizable: Gives full control to the programmer. You manually define how each field is written to and read from the stream.
2. Marker Interface vs Custom Methods
Serializable: A marker interface with no methods. Simply implementing it marks your class as serializable.
Externalizable: Extends Serializable and provides two mandatory methods:
writeExternal(ObjectOutput out)readExternal(ObjectInput in)
3. Constructor Calls During Deserialization
Serializable: No constructor of the serialized class is invoked during deserialization.
Externalizable: Requires a public no-argument constructor, which is executed first by the JVM before calling readExternal().
4. Impact of the transient Modifier
Serializable: Fields marked with transient are skipped during serialization.
Externalizable: The transient keyword has no effect because you explicitly write and read fields inside the custom methods.
Quick Reference Summary
| Feature | Serializable | Externalizable |
|---|---|---|
| Control Level | JVM Default (Automatic) | Developer Managed (Manual) |
| Methods | 0 (Marker Interface) | 2 (writeExternal, readExternal) |
| Constructor Needed | None invoked | Public no-arg constructor required |
transient Keyword |
Supported | Ignored |
Related Tutorials from Ram N Java
Click here to watch in Youtube :
https://www.youtube.com/watch?v=9ErelaR_u5I&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge
![]() |
| Java Tutorial : Java IO (Java serializable vs externalizable | serialization vs externalization -V1) |

No comments:
Post a Comment