Friday, 23 September 2016

Java Tutorial : Java IO (Java Serialization_V3)

🚀 Enjoying Java Tutorials?

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

🔔 Subscribe to Ram N Java

Java Serialization Overview

In Java, objects exist in heap memory as long as the JVM is running. But what happens when you need to store an object’s current state to a file, persist it into a database, or transfer it across the network to another system? This is where Java Serialization comes into play.

Serialization vs. Deserialization

  • Serialization: The mechanism of translating an in-memory Java object into a platform-independent byte stream.
  • Deserialization: The reverse operation, where the byte stream is reconstructed back into a live Java object in memory.

The Core Building Blocks

1. The java.io.Serializable Interface

A class must implement the java.io.Serializable marker interface to become serializable. Because it contains no methods or variables, it simply marks the class as eligible for conversion to a byte stream.

2. ObjectOutputStream & ObjectInputStream

The standard stream classes responsible for serialization and deserialization:

  • ObjectOutputStream.writeObject() writes the object state to an output stream.
  • ObjectInputStream.readObject() reconstructs the object from an input stream.

3. transient and static Keywords

  • transient: Prevents specific instance fields from being serialized (restored to default values like null or 0).
  • static: Belongs to the class level rather than the instance, meaning static variables are not part of the serialized state.

Why Use Java Serialization?

  • Data Persistence: Easily preserve object graphs on disk between application runs.
  • Network Communication: Essential for Java RMI (Remote Method Invocation) and distributed messaging.
  • Deep Copying: Create complete clones of complex object graphs with minimal boilerplate code.

Related Tutorials from Ram N Java


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

Click the below Image to Enlarge
Java Tutorial : Java IO (Java Serialization_V3) 
Java Tutorial : Java IO (Java Serialization_V3) 
Java Tutorial : Java IO (Java Serialization_V3) 
Java Tutorial : Java IO (Java Serialization_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