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

Saturday, 4 February 2023

Java Program to Send/Consume Custom Object into/from Kafka Server running on Amazon EC2 Instance

🚀 Master Cloud Kafka Integration!

Subscribe to Ram N Java for simplified tutorials on AWS, Apache Kafka, and Advanced Java Backend Development!

SUBSCRIBE TO OUR CHANNEL

Kafka on AWS: Sending & Consuming Custom Objects

Moving beyond simple strings is essential for real-world applications. In this tutorial, we "simplify" the process of Sending and Consuming Custom Objects using a Kafka Cluster running on Amazon EC2.

Advanced Producer & Consumer Workflow

We walk through the end-to-end technical setup required to handle complex data types in a cloud environment:

  • Custom Serialization: Implementing Serializer and Deserializer for your Java objects (e.g., a Student object) using Jackson.
  • AWS EC2 Setup: Configuring your Kafka server on Amazon EC2 to accept external traffic from your local Java application.
  • Producer Implementation: Writing the Java code to push complex objects to a specific Kafka topic.
  • Consumer Implementation: Building the listener that retrieves and reconstructs those objects for processing.

Critical Skills for Cloud Engineers

For Java Developers and DevOps Engineers, integrating local applications with cloud-based messaging is a core requirement. We discuss the configuration details like advertised.listeners and security group settings that are vital for AWS EC2 connectivity. This guide bridges the gap between local development and Enterprise Cloud Architecture.

Practical Clarity for Complex Data

Handling JSON or custom POJOs in Kafka doesn't have to be intimidating. This tutorial provides the code-heavy, practical walkthrough you need to master Object Serialization in a distributed environment. Join us at Ram N Java and take your Apache Kafka expertise to the cloud.

📥 Level Up Your Cloud Skills!

Watch the full video for the complete Java implementation and AWS configuration. Subscribe to Ram N Java for more high-quality tech guides and simplified backend tutorials!

Java Program to Send Custom Object into Kafka Topic & Consume Custom Object From Kafka Topic

🚀 Master Object Serialization!

Subscribe to Ram N Java for simplified tutorials on Apache Kafka, Custom Serializers, and Advanced Java Backend!

SUBSCRIBE TO OUR CHANNEL

Kafka Java: Sending & Consuming Custom POJOs

While strings are simple, real-world enterprise applications deal with complex data. In this tutorial, we "simplify" the process of Sending and Consuming Custom Objects (POJOs) using Apache Kafka and Java.

Handling Complex Data Types

We walk through the complete implementation for moving structured data through your Kafka cluster:

  • Custom Serializer: Implementing the Serializer interface to convert Java objects into byte arrays using Jackson.
  • Custom Deserializer: Implementing the Deserializer interface to reconstruct your objects on the consumer side.
  • Producer Configuration: Setting up the KafkaProducer to use your custom class for value serialization.
  • Consumer Implementation: Building the polling loop that receives and casts data back to your custom Java class.

Essential for Microservices

For Java Developers, the ability to send domain objects like User, Order, or Product directly through Kafka is a fundamental skill. We show you how to leverage the Jackson ObjectMapper to handle JSON conversion seamlessly, ensuring your Event-Driven Architecture is robust and type-safe. This guide provides the practical code you need for real-world development.

Practical Code-First Learning

Don't get stuck with just text messages. This tutorial provides the technical clarity to handle any data structure in Apache Kafka. By walking through a concrete example with a "Student" object, we make complex serialization accessible. Join us at Ram N Java and elevate your backend programming skills.

📥 Start Building!

Watch the full video to see the step-by-step Java implementation. Subscribe to Ram N Java for more high-quality tech guides and simplified backend tutorials!

Wednesday, 30 November 2022

Spring Boot with Spring Kafka Producer Example | Apache Kafka Publisher Example using SpringBoot

🚀 Build Production-Ready Producers!

Subscribe to Ram N Java for simplified tutorials on Spring Boot, Apache Kafka, and Enterprise System Architecture!

SUBSCRIBE TO OUR CHANNEL

Spring Boot & Kafka: A Deep Dive into Producer Logic

Efficiently sending data to a distributed log is the first step in any streaming pipeline. In this tutorial, we "simplify" the Spring Boot Kafka Producer, taking you from initial setup to a production-ready implementation.

Inside the Producer Pipeline

We explore the technical building blocks required to publish reliable messages within the Spring ecosystem:

  • KafkaTemplate Architecture: Deep diving into how Spring's KafkaTemplate simplifies message delivery and handles synchronization under the hood.
  • Serialization Strategies: Configuring key and value serializers to ensure your data is correctly formatted for the Kafka broker.
  • Advanced Configuration: Fine-tuning application.properties for bootstrap servers, acknowledgments (acks), and retries.
  • Producer Factory Setup: Understanding the role of ProducerFactory in managing the lifecycle of your Kafka producers.

Mastering Event-Driven Design

For Java Developers, mastering the producer side is critical for building high-throughput Event-Driven Architectures. We show you how Spring Boot removes the complexity of the native Kafka API, allowing you to focus on your application's core business logic. This guide provides the conceptual and technical clarity needed to scale your Microservices effectively.

Practical Implementation & Results

Watch as we build a complete producer from scratch and demonstrate real-time message publishing to a local Kafka cluster. This deep dive gives you a robust template that you can confidently deploy in a professional environment. Join us at Ram N Java and take your Spring Boot and Kafka skills to the next level.

📥 Build Your Pipeline!

Watch the full video to see the step-by-step Java code walkthrough and live demonstration. Subscribe to Ram N Java for more high-quality tech guides and simplified backend tutorials!

Tuesday, 4 October 2022

Apache Kafka - Create a Simple Producer in Java | Java Kafka Producer code | Java with Apache Kafka

🚀 Master Kafka Development!

Subscribe to Ram N Java for simplified tutorials on Java, Apache Kafka, and Backend Engineering!

SUBSCRIBE TO OUR CHANNEL

Java Kafka Producer: A Step-by-Step Implementation Guide

Publishing data to a distributed stream is a fundamental skill for any modern developer. In this tutorial, we "simplify" the creation of an Apache Kafka Producer in Java, walking you through the exact four steps needed to start sending messages to a Kafka topic.

The 4 Essential Steps to Produce Messages

We break down the technical workflow required to build a functioning producer from scratch:

  • Step 1: Producer Properties: Setting up the Properties object with vital configurations like bootstrap.servers, key.serializer, and value.serializer.
  • Step 2: Create Producer: Initializing the KafkaProducer object by passing your custom properties.
  • Step 3: Create Producer Record: Defining the ProducerRecord which specifies the destination topic and the message data.
  • Step 4: Send Data: Using the send() method to dispatch your record asynchronously to the Kafka cluster.

Critical Logic: Flush and Close

For Java Developers, understanding the asynchronous nature of Kafka is key. We explain why calling flush() and close() in a finally block is mandatory. Without these, the main thread may exit before the data is actually pushed from the internal buffer to the server. This guide provides the conceptual clarity to ensure Data Integrity in your Microservices.

Live Demo & CLI Integration

Watch the complete end-to-end flow: from starting Zookeeper and Kafka servers to creating a multi-partition topic via CLI, and finally running our Java program. We even show a live consumer catching the messages in real-time! Join us at Ram N Java and elevate your Apache Kafka expertise today.

📥 Get the Source Code!

Watch the full video to see the code walkthrough and live demonstration. Subscribe to Ram N Java for more high-quality tech guides and simplified backend tutorials!

Friday, 23 April 2021

Returning Java Object as Return Value - RESTful Web Services with Spring framework | Spring Boot

🚀 Build Modern APIs!

Subscribe to Ram N Java for simplified Java, Spring Boot, and REST API tutorials!

SUBSCRIBE TO OUR CHANNEL

Returning Java Objects in REST APIs

In this tutorial, we explore the core of RESTful Web Services: returning Java objects as JSON responses. We demonstrate how Spring Boot seamlessly handles data serialization, making it incredibly easy to build data-driven applications.

The Magic of Serialization

Spring Boot uses the Jackson library under the hood to convert your Java POJOs into JSON. We walk through the process of creating a clean data model and exposing it through a controller:

  • @RestController: Understanding how this annotation combines @Controller and @ResponseBody to simplify your API development.
  • POJO Design: Best practices for creating simple Java objects that map perfectly to your API responses.
  • Automatic Conversion: See how Spring automatically sets the correct Content-Type to application/json.

Live Practical Session

We build a simple Employee model and a corresponding EmployeeController from scratch. You'll see exactly how to return single objects and lists of objects, and how they appear when requested via a web browser or Postman.

Why Master This?

Mastering how to return objects is the first step in building any real-world backend. It's the foundation for microservices, mobile app backends, and modern web applications. By understanding these Spring Boot fundamentals, you're well on your way to becoming a professional Java developer.

📥 Get the Source Code!

I have shared the full Java source code for this tutorial! Check out the download links in the YouTube video description above to get started with the code.

Monday, 10 June 2019

How to Send/Receive Product object to/from Queue using Java program? | RabbitMQ tutorial

🚀 Master Java Messaging!

Subscribe to Ram N Java for simplified Java, RabbitMQ, and Spring Boot tutorials!

SUBSCRIBE TO OUR CHANNEL

Sending Java Objects to RabbitMQ

Modern applications need to exchange complex data structures, not just simple text. In this tutorial, we "simplify" the process of sending and receiving custom Java Objects through RabbitMQ queues using the Java client library.

Object Serialization Simplified

To send an object over the wire, it must first be converted into a format RabbitMQ understands. We walk you through the core concepts of object handling in a messaging environment:

  • Defining Your POJO: Creating a simple, serializable Java class to represent your data (like an Employee or Order).
  • Serialization: Using Java's built-in serialization or libraries like Jackson to convert objects into byte arrays for transmission.
  • Deserialization: How the consumer reconstructs the original Java object from the received message bytes.

The Producer and Consumer Logic

We demonstrate a complete working example of a Producer sending a Java object and a Consumer waiting to receive it. You'll learn how to manage connections, channels, and queues while ensuring that your data remains intact throughout the entire messaging lifecycle. This hands-on approach shows you the "under-the-hood" mechanics of Java messaging.

A Vital Skill for Java Developers

Mastering Object Messaging with RabbitMQ is essential for building decoupled and distributed Java applications. It allows different parts of your system to share complex state without being directly linked. Whether you're working on microservices or legacy system integration, this skill is a cornerstone of professional Backend Development.

📥 Download the Source Code!

The complete Java source code and PowerPoint presentation for this object messaging tutorial are available! Check the download links in the YouTube video description above to get started.

Friday, 14 October 2016

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

🚀 Enjoying Java Tutorials?

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

🔔 Subscribe to Ram N Java

Introduction

When persisting objects or transmitting them over a network in Java, developers often face a common question: should you use default Serialization or opt for Externalization? While both mechanisms achieve object persistence, understanding their architectural differences helps you build faster, more memory-efficient applications.

Core Differences Between Serializable and Externalizable

1. Default Mechanism vs Custom Implementation

Serializable: Managed automatically by the Java Virtual Machine (JVM). The JVM takes full responsibility for writing out the object hierarchy and fields.

Externalizable: Gives total control to the developer. You determine exactly what data gets written to the stream and restored from it.

2. Method Requirements

Serializable: It is a marker interface (contains no methods). Simply tagging your class tells Java it can be serialized.

Externalizable: Extends Serializable and provides two abstract methods that you must implement:

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

3. Constructor Execution on Deserialization

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

Externalizable: A public no-argument constructor is mandatory. The JVM executes this constructor first to instantiate the object before calling readExternal().

4. Transient Keyword Behavior

Serializable: Variables marked with transient are ignored by the default serialization process.

Externalizable: The transient keyword is ignored because the programmer explicitly decides which variables to write and read.

Quick Comparison Summary

Feature Serializable Externalizable
Control Level JVM Default (Automatic) Developer Controlled (Manual)
Method Count 0 (Marker Interface) 2 (writeExternal / readExternal)
Constructor Call None executed for the class 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=hqfdRb7dP18&list=UUhwKlOVR041tngjerWxVccw

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

    🚀 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 can be achieved using two main interfaces from the java.io package: Serializable and Externalizable. While both allow objects to be saved or sent over a network, they work in fundamentally different ways when it comes to JVM automation, performance, and customization.

    Key Differences: Serializable vs Externalizable

    1. Default vs Manual Serialization

    Serializable: Managed entirely by the JVM's default mechanism. It automatically serializes the object's non-transient state without requiring custom code.

    Externalizable: Gives full control to the programmer. You manually define how each field is written to and read from the stream.

    2. Marker Interface vs Custom Methods

    Serializable: A marker interface with no methods. Simply implementing it marks your class as serializable.

    Externalizable: Extends Serializable and provides two mandatory methods:

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

    3. Constructor Calls During Deserialization

    Serializable: No constructor of the serialized class is invoked during deserialization.

    Externalizable: Requires a public no-argument constructor, which is executed first by the JVM before calling readExternal().

    4. Impact of the transient Modifier

    Serializable: Fields marked with transient are skipped during serialization.

    Externalizable: The transient keyword has no effect because you explicitly write and read fields inside the custom methods.

    Quick Reference Summary

    Feature Serializable Externalizable
    Control Level JVM Default (Automatic) Developer Managed (Manual)
    Methods 0 (Marker Interface) 2 (writeExternal, readExternal)
    Constructor Needed None invoked 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=9ErelaR_u5I&list=PLmCsXDGbJHdirIE37zMHVPk5SYWmqfeZC

    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 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
  • Thursday, 13 October 2016

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

    🚀 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 can be achieved using two main interfaces from the java.io package: Serializable and Externalizable. While both allow objects to be saved or sent over a network, they work in fundamentally different ways when it comes to JVM automation, performance, and customization.

    Key Differences: Serializable vs Externalizable

    1. Default vs Manual Serialization

    Serializable: Managed entirely by the JVM's default mechanism. It automatically serializes the object's non-transient state without requiring custom code.

    Externalizable: Gives full control to the programmer. You manually define how each field is written to and read from the stream.

    2. Marker Interface vs Custom Methods

    Serializable: A marker interface with no methods. Simply implementing it marks your class as serializable.

    Externalizable: Extends Serializable and provides two mandatory methods:

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

    3. Constructor Calls During Deserialization

    Serializable: No constructor of the serialized class is invoked during deserialization.

    Externalizable: Requires a public no-argument constructor, which is executed first by the JVM before calling readExternal().

    4. Impact of the transient Modifier

    Serializable: Fields marked with transient are skipped during serialization.

    Externalizable: The transient keyword has no effect because you explicitly write and read fields inside the custom methods.

    Quick Reference Summary

    Feature Serializable Externalizable
    Control Level JVM Default (Automatic) Developer Managed (Manual)
    Methods 0 (Marker Interface) 2 (writeExternal, readExternal)
    Constructor Needed None invoked 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=9ErelaR_u5I&list=UUhwKlOVR041tngjerWxVccw

    Click the below Image to Enlarge
    Java Tutorial : Java IO (Java serializable vs externalizable | serialization vs externalization -V1) 
    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
  • Wednesday, 12 October 2016

    Java Tutorial : Java IO (Java Serialization - How to serialize primitive values)

    🚀 Enjoying Java Tutorials?

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

    🔔 Subscribe to Ram N Java

    Introduction

    While Java serialization is widely recognized for saving objects, you frequently need to write and restore raw primitive values—such as int, boolean, double, or long—directly to an output stream. In this guide, we break down how Java IO handles primitive serialization using ObjectOutputStream and ObjectInputStream.

    Writing and Reading Primitive Data

    ObjectOutputStream implements the DataOutput interface, providing dedicated methods to write primitive values directly into the byte stream without wrapping them in complex custom objects.

    Common Methods for Writing Primitives

    • writeInt(int val): Writes a 32-bit integer.
    • writeDouble(double val): Writes a 64-bit double-precision floating point number.
    • writeBoolean(boolean val): Writes a boolean value as a 1-byte value.
    • writeUTF(String str): Writes a string using modified UTF-8 encoding.

    Restoring Primitives with ObjectInputStream

    To read the primitives back, ObjectInputStream implements DataInput and provides matching methods:

    • readInt()
    • readDouble()
    • readBoolean()
    • readUTF()

    Key Rules to Remember

    Order Matters: Primitive values must be read back in the exact same sequence and type order in which they were written. Reading an int when a double was written will result in corrupted data or stream errors.

    Mixing Objects and Primitives: You can seamlessly combine calls to writeObject() and primitive write methods like writeInt() on the same stream, provided you mirror the operations during deserialization.

    Related Tutorials from Ram N Java


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

    Serialization.java
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    
    public class Serialization
    {
    
        public static void main(String[] args) throws FileNotFoundException,
                IOException, ClassNotFoundException
        {
            Serialization serialization = new Serialization();
            serialization.writePrimitiveValues();
        }
    
        private void writePrimitiveValues() throws FileNotFoundException,
                IOException
        {
            FileOutputStream fileOutputStream = null;
            ObjectOutputStream objectOutputStream = null;
            try
            {
                fileOutputStream = new FileOutputStream("myfile.txt");
                objectOutputStream = new ObjectOutputStream(fileOutputStream);
    
                objectOutputStream.writeInt(10);
                objectOutputStream.writeDouble(50.55);
                objectOutputStream.writeBoolean(true);
                
                System.out
                        .println("Successfully written primitive values to the file.\n");
            }
            finally
            {
    
                if (objectOutputStream != null)
                {
                    /*
                     * Closing a ObjectOutputStream will also
                     * close the OutputStream instance to which
                     * the ObjectOutputStream is writing.
                     */
                    objectOutputStream.close();
                }
            }
    
        }
    
    }
    
    Output
    Successfully written primitive values to the file.

    myfile.txt

    DeSerialization.java
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    
    public class DeSerialization
    {
    
        public static void main(String[] args) throws FileNotFoundException,
                IOException, ClassNotFoundException
        {
            DeSerialization deSerialization = new DeSerialization();
            deSerialization.readPrimitiveValues();
        }
    
        private void readPrimitiveValues() throws IOException,
                FileNotFoundException, ClassNotFoundException
        {
            FileInputStream fileInputStream = null;
            ObjectInputStream objectInputStream = null;
    
            try
            {
                fileInputStream = new FileInputStream("myfile.txt");
                objectInputStream = new ObjectInputStream(fileInputStream);
    
                System.out
                        .println("Successfully read primitive values from the file.\n");
    
                int intValue = objectInputStream.readInt();
                double doubleValue = objectInputStream.readDouble();
                boolean booleanValue = objectInputStream.readBoolean();
    
                System.out.println("intValue = " + intValue);
                System.out.println("doubleValue = " + doubleValue);
                System.out.println("booleanValue = " + booleanValue);
    
            }
            finally
            {
    
                if (objectInputStream != null)
                {
                    /*
                     * Closing a ObjectInputStream will also
                     * close the InputStream instance from which
                     * the ObjectInputStream is reading.
                     */
                    objectInputStream.close();
                }
            }
    
        }
    
    }
    
    Output
    Successfully read primitive values from the file.
    
    intValue = 10
    doubleValue = 50.55
    booleanValue = true
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_Serialization_primitive_App.zip?attredirects=0&d=1

    Github Link:
    https://github.com/ramram43210/Java/tree/master/BasicJava/JavaIODemo_Serialization_primitive_App

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/9f822df4c81f286daa095d86846058f3a9c16303/BasicJava/JavaIODemo_Serialization_primitive_App/?at=master

    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 - serialVersionUID)

    🚀 Enjoying Java Tutorials?

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

    🔔 Subscribe to Ram N Java

    Introduction to serialVersionUID

    When you serialize an object in Java, how does the runtime make sure that the class used during deserialization matches the class used during serialization? The answer lies in a special identifier known as serialVersionUID. It functions as a unique version stamp for every serializable class.

    Why is serialVersionUID Important?

    1. Version Control for Objects

    During deserialization, the Java runtime verifies whether the serialVersionUID sent with the serialized stream matches the serialVersionUID of the local class loaded in memory. If the two values match, deserialization succeeds smoothly.

    2. Preventing InvalidClassException

    If the UID in the stream does not match the UID of the current class definition, the JVM throws an InvalidClassException. This prevents loading corrupted data into incompatible class definitions.

    3. Automatic vs Explicit Declaration

    If you do not declare a serialVersionUID explicitly, the compiler generates one automatically based on class details (such as fields, methods, and modifiers). However, even a minor change to the class structure alters this computed hash, breaking compatibility with previously saved streams.

    Declaring it explicitly gives you full control over class evolution:

    private static final long serialVersionUID = 1L;

    Best Practices

    • Always declare an explicit serialVersionUID in any class implementing Serializable.
    • Keep the modifier signature strictly as private static final long.
    • Update the UID only when introducing incompatible changes that should reject older serialized objects.

    Related Tutorials from Ram N Java


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

    Click the below Image to Enlarge
    Java Tutorial : Java IO (Java Serialization - serialVersionUID) 
    Java Tutorial : Java IO (Java Serialization - serialVersionUID) 
    Java Tutorial : Java IO (Java Serialization - serialVersionUID) 
    Java Tutorial : Java IO (Java Serialization - serialVersionUID) 
    Java Tutorial : Java IO (Java Serialization - serialVersionUID) 
    Java Tutorial : Java IO (Java Serialization - serialVersionUID) 
    Java Tutorial : Java IO (Java Serialization - serialVersionUID) 
    Java Tutorial : Java IO (Java Serialization - serialVersionUID) 
    Employee.java
    import java.io.Serializable;
    
    public class Employee implements Serializable
    {
        private static final long serialVersionUID = 4676072847340124979L;
        private int id;
        private String name;
    
        public Employee(int id, String name)
        {
            super();
            this.id = id;
            this.name = name;
        }
    
        public int getId()
        {
            return id;
        }
    
        public void setId(int id)
        {
            this.id = id;
        }
    
        public String getName()
        {
            return name;
        }
    
        public void setName(String name)
        {
            this.name = name;
        }
    
    }
    
    Serialization.java
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    
    public class Serialization
    {
    
        public static void main(String[] args) throws FileNotFoundException,
                IOException, ClassNotFoundException
        {
            Serialization serialization = new Serialization();
            serialization.writeEmployeeObject();
        }
    
        private void writeEmployeeObject() throws FileNotFoundException,
                IOException
        {
            FileOutputStream fileOutputStream = null;
            ObjectOutputStream objectOutputStream = null;
            try
            {
                fileOutputStream = new FileOutputStream("employee.ser");
                objectOutputStream = new ObjectOutputStream(fileOutputStream);
    
                Employee employee = new Employee(101, "Peter");
    
                /*
                 * Write the specified object to the
                 * ObjectOutputStream.
                 */
                objectOutputStream.writeObject(employee);
                System.out
                        .println("Successfully written employee object to the file.\n");
            }
            finally
            {
    
                if (objectOutputStream != null)
                {
                    /*
                     * Closing a ObjectOutputStream will also
                     * close the OutputStream instance to which
                     * the ObjectOutputStream is writing.
                     */
                    objectOutputStream.close();
                }
            }
    
        }
    
    }
    Output
    Successfully written employee object to the file.
    
    
    
    employee.ser

    DeSerialization.java
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    
    public class DeSerialization
    {
    
        public static void main(String[] args) throws FileNotFoundException,
                IOException, ClassNotFoundException
        {
            DeSerialization deSerialization = new DeSerialization();
            deSerialization.readEmployeeObject();
        }
    
        private void readEmployeeObject() throws IOException,
                FileNotFoundException, ClassNotFoundException
        {
            FileInputStream fileInputStream = null;
            ObjectInputStream objectInputStream = null;
    
            try
            {
                fileInputStream = new FileInputStream("employee.ser");
                objectInputStream = new ObjectInputStream(fileInputStream);
    
                /*
                 * Read an object from the ObjectInputStream.
                 */
                Employee employee = (Employee) objectInputStream.readObject();
                System.out
                        .println("Successfully read employee object from the file.\n");
    
                System.out.println("Id  = " + employee.getId());
                System.out.println("Name = " + employee.getName());
    
            }
            finally
            {
    
                if (objectInputStream != null)
                {
                    /*
                     * Closing a ObjectInputStream will also
                     * close the InputStream instance from which
                     * the ObjectInputStream is reading.
                     */
                    objectInputStream.close();
                }
            }
    
        }
    
    }
    
    Output
    Successfully read employee object from the file.
    
    Id  = 101
    Name = Peter
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_Serialization_serialVersionUID_App.zip?attredirects=0&d=1

    Github Link:
    https://github.com/ramram43210/Java/tree/master/BasicJava/JavaIODemo_Serialization_serialVersionUID_App

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/9f822df4c81f286daa095d86846058f3a9c16303/BasicJava/JavaIODemo_Serialization_serialVersionUID_App/?at=master

    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:
    Older Posts Home

    Tutorials