Friday, 14 October 2016

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

🚀 Enjoying Java Tutorials?

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

🔔 Subscribe to Ram N Java

Introduction

In core Java development, preserving object state or transferring data across networks requires serialization. Java provides two mechanisms to handle this: the Serializable interface and the Externalizable interface. Knowing how each works and when to pick one over the other is an essential skill for writing clean, optimized code and answering technical interview questions.

Key Differences: Serializable vs Externalizable

1. Default vs Custom Control

Serializable: Managed automatically by the Java Virtual Machine (JVM). The JVM inspects the object graph using reflection and writes all non-transient state by default.

Externalizable: Places total control in your hands. You write explicit code specifying exactly which data fields are saved and restored.

2. Marker Interface vs Explicit Methods

Serializable: A marker interface that contains no methods. Implementing it simply grants permission to the JVM to serialize instances of the class.

Externalizable: Extends Serializable and requires two methods to be implemented:

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

3. Constructor Invocation on Deserialization

Serializable: The serialized class constructor is not executed during object reconstruction.

Externalizable: A public no-argument constructor is strictly required. The JVM calls it to instantiate the object before executing readExternal().

4. Role of the transient Modifier

Serializable: Fields marked with transient are skipped automatically during serialization.

Externalizable: The transient modifier has no effect because you explicitly control which fields are saved and restored inside your custom methods.

Quick Reference Summary

Feature Serializable Externalizable
Control Automatic (JVM default) Manual (Developer custom)
Methods 0 (Marker interface) 2 (writeExternal, readExternal)
Constructor Needed None called 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=0C2vhOyYp7Y&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial : Java IO (Java serializable vs externalizable | serialization vs externalization -V2) 
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