Friday, 14 October 2016

Java Tutorial : Java IO (Java serializable vs externalizable | serialization vs externalization -V3)

🚀 Enjoying Java Tutorials?

Subscribe to Ram N Java for clean, beginner-friendly core Java tutorials and interview prep!

🔔 Subscribe to Ram N Java

Introduction

When persisting objects or transmitting them over a network in Java, developers often face a common question: should you use default Serialization or opt for Externalization? While both mechanisms achieve object persistence, understanding their architectural differences helps you build faster, more memory-efficient applications.

Core Differences Between Serializable and Externalizable

1. Default Mechanism vs Custom Implementation

Serializable: Managed automatically by the Java Virtual Machine (JVM). The JVM takes full responsibility for writing out the object hierarchy and fields.

Externalizable: Gives total control to the developer. You determine exactly what data gets written to the stream and restored from it.

2. Method Requirements

Serializable: It is a marker interface (contains no methods). Simply tagging your class tells Java it can be serialized.

Externalizable: Extends Serializable and provides two abstract methods that you must implement:

  • writeExternal(ObjectOutput out)
  • readExternal(ObjectInput in)

3. Constructor Execution on Deserialization

Serializable: No constructor is invoked for the serialized class during the deserialization process.

Externalizable: A public no-argument constructor is mandatory. The JVM executes this constructor first to instantiate the object before calling readExternal().

4. Transient Keyword Behavior

Serializable: Variables marked with transient are ignored by the default serialization process.

Externalizable: The transient keyword is ignored because the programmer explicitly decides which variables to write and read.

Quick Comparison Summary

Feature Serializable Externalizable
Control Level JVM Default (Automatic) Developer Controlled (Manual)
Method Count 0 (Marker Interface) 2 (writeExternal / readExternal)
Constructor Call None executed for the class 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=hqfdRb7dP18&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial : Java IO (Java serializable vs externalizable | serialization vs externalization -V3) 
Java Tutorial : Java IO (Java serializable vs externalizable | serialization vs externalization -V3) 
See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment

    Tutorials