Monday, 17 October 2016

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

🚀 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, converting an object into a byte stream so that it can be saved to a file, database, or sent across a network is called Serialization. When it comes to implementation, Java gives us two choices: Serializable and Externalizable. Understanding their core differences is essential for writing efficient applications and clearing Java technical interviews.

Key Differences: Serializable vs Externalizable

1. Level of Control

Serialization (Serializable): The JVM handles the process automatically. The entire object state is converted to bytes by default without manual intervention.

Externalization (Externalizable): The programmer has complete manual control over what data gets written and read, allowing customized serialization logic.

2. Marker Interface vs Methods

Serializable: It is a marker interface (contains no methods). Simply implementing it signals to the JVM that your class can be serialized.

Externalizable: It extends Serializable and defines two required methods:

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

3. Object Creation During Deserialization

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

Externalizable: A public no-argument constructor is executed first to create the object instance before restoring fields.

4. Role of the transient Keyword

Serializable: Variables declared with the transient keyword are excluded from default serialization.

Externalizable: The transient keyword has no impact. The programmer explicitly writes and reads variables in writeExternal() and readExternal() regardless of whether they are marked transient.

Quick Comparison Summary

Feature Serializable Externalizable
Control JVM-controlled Programmer-controlled
Methods 0 (Marker interface) 2 (writeExternal, readExternal)
Constructor Call None executed 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=-LN1Mr_4SM4&list=UUhwKlOVR041tngjerWxVccw

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