Monday, 17 October 2016

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

🚀 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 Java IO, converting an object into a sequence of bytes is a fundamental capability. Java gives developers two distinct interfaces to achieve this: Serializable and Externalizable. Deciding between them comes down to whether you prefer automated simplicity or complete custom control over your object's serialized format.

Key Differences: Serializable vs Externalizable

1. Default vs Custom Control

Serializable: The entire serialization and deserialization process is handled automatically by the JVM. The virtual machine determines how fields are written and retrieved.

Externalizable: Control is shifted entirely to the developer. You define the explicit logic for writing and restoring each field, preventing unwanted metadata from being persisted.

2. Methods Present in the Interface

Serializable: A marker interface with zero methods. Simply implementing it marks the class as eligible for JVM default serialization.

Externalizable: Extends Serializable and requires two explicit callback methods:

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

3. Constructor Behavior During Deserialization

Serializable: The constructor of the serialized class is never called during deserialization.

Externalizable: Deserialization mandates a public no-argument constructor to instantiate the object before invoking readExternal().

4. Use of the transient Modifier

Serializable: Any field declared with the transient keyword is automatically excluded from default serialization.

Externalizable: The transient keyword has no impact because field persistence is explicitly coded inside writeExternal() and readExternal().

Quick Reference Summary

Concept Serializable Externalizable
Control Level Automatic (JVM managed) Custom (Developer managed)
Methods None (Marker interface) 2 (writeExternal / readExternal)
Constructor Needed No constructor called Public no-argument constructor required
Transient Keyword Supported Not applicable

Related Tutorials from Ram N Java


Click here to watch in Youtube :
https://www.youtube.com/watch?v=gpl_ZmY4A4Y&list=UUhwKlOVR041tngjerWxVccw

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