Showing posts with label Java Serialization. Show all posts
Showing posts with label Java Serialization. Show all posts

Tuesday, 18 October 2016

Java Tutorial : Java IO (Java Serialization of Complex Objects)

Enjoying Java Tutorials on Ram N Java?

Subscribe now for simplified, step-by-step programming guides and tutorials!

SUBSCRIBE TO RAM N JAVA

What Is Serialization in Java?

Serialization is the process of converting an object's state into a byte stream. This byte stream can then be saved to a file, stored in a database, or transmitted across a network. Deserialization does the exact opposite: it reconstructs the original object from the byte stream.

Handling Complex Object Graphs

In real-world Java applications, objects rarely exist alone. A single object frequently holds references to other objects, creating an object graph:

  • Suppose you have Object A, which contains references to Object B and Object C.
  • In turn, Object B references Object D and Object E.

When you pass the top-level Object A to the writeObject() method, Java's serialization engine automatically navigates the entire graph. All interconnected objects (A, B, C, D, and E) are written to the output stream together.

Rebuilding the Objects with Deserialization

When reading back using readObject(), Java handles reconstructing the complete network of objects. Crucially, the original object references and relationships are preserved exactly as they were created, ensuring your program's state remains intact.

Watch More Related Java Tutorials

Check out these related Java Serialization tutorials from our channel:


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

Click the below Image to Enlarge
Java Tutorial : Java IO (Java Serialization of Complex Objects) 
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
  • Java Tutorial : Java IO (Java Serialization and Deserialization notes)

    Learn Core Java on Ram N Java!

    Subscribe today for concise, clear, and practical Java tutorials!

    SUBSCRIBE TO RAM N JAVA

    1. The Transient Keyword and Default Values

    When you serialize an object, you might not want every single field saved. If you declare a field as transient, Java will skip it during serialization.

    Upon deserialization, transient fields automatically receive their language defaults:

    • Object references (like String) are set to null.
    • Numeric types (like int) are initialized to 0.
    • Boolean fields are initialized to false.

    2. Why Static Variables Are Not Serialized

    A common question in Java interviews is whether static fields are serialized. The short answer is no. Serialization preserves the state of a specific object instance. Because static variables belong to the class itself rather than any individual instance, they are not saved into the byte stream.

    3. Object Graph Traversal & NotSerializableException

    During serialization, the runtime navigates the entire object graph. If an encountered object does not implement java.io.Serializable, the JVM throws a NotSerializableException indicating the offending class.

    The Fix: If you cannot modify that class to make it serializable, declare the reference holding that object as transient.

    Watch More Related Java Tutorials

    Deepen your understanding of Java IO and serialization with these tutorials:


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

    Click the below Image to Enlarge 
    Java Tutorial : Java IO (Java Serialization and Deserialization notes)
    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
  • Monday, 17 October 2016

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

    Master Core Java with Ram N Java!

    Subscribe today for in-depth, easy-to-follow Java tutorials and core concepts!

    SUBSCRIBE TO RAM N JAVA

    The Limitations of Standard Serializable

    The standard Serializable interface is a marker interface that tells the JVM to handle serialization automatically. While convenient, it comes with key drawbacks:

    • Lack of Control: The JVM serializes all non-transient fields across the entire class hierarchy, including metadata, which consumes unnecessary bandwidth and storage.
    • All-or-Nothing Deserialization: You cannot selectively choose which fields to deserialize.
    • Performance Overhead: Calculating default serialVersionUID values across complex inheritance trees can impact runtime performance.

    Externalizable: Complete Control Over Serialization

    To take full command of the serialization process, Java provides the java.io.Externalizable interface. Unlike Serializable, it gives developers direct responsibility over what data gets written to and read from the stream:

    • writeExternal(ObjectOutput out): Define the exact fields and format to serialize.
    • readExternal(ObjectInput in): Restore only the required fields in the matching order.

    Key Requirements for Externalizable

    When implementing Externalizable in your Java classes, keep these core rules in mind:

    • Your class must provide a public no-argument (zero-arg) constructor, which the JVM invokes during deserialization before calling readExternal.
    • Superclass state is not automatically written—you must explicitly manage inheritance if required.

    Watch More Related Java Tutorials

    Explore more Java serialization concepts from our channel:


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

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