Master Java Serialization & Deserialization: Core Concepts and Implementation

This article explains Java serialization and deserialization fundamentals, why they are needed, the underlying algorithms, the JDK APIs, step‑by‑step code examples, important considerations, and best practices for safely persisting and transmitting object state.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Master Java Serialization & Deserialization: Core Concepts and Implementation

1. Basic Concepts

What are serialization and deserialization? Serialization converts a Java object into a byte sequence; deserialization restores the object from that byte sequence.

Serialization preserves an object's state for storage or network transmission, while deserialization rebuilds the object from the stored bytes.

In essence, serialization writes an object's state to an ordered byte stream; deserialization reads the stream to reconstruct the object.

Why use them? They enable persistent storage, remote communication, and inter‑process object transfer by converting objects to a portable byte format.

Permanent object storage (e.g., files or databases)

Object transmission over networks as byte streams

Inter‑process object passing

2. How Java Implements Serialization & Deserialization

JDK APIs java.io.ObjectOutputStream: provides writeObject(Object obj) to serialize an object to an output stream. java.io.ObjectInputStream: provides readObject() to deserialize bytes from an input stream back into an object.

Requirements

Only classes that implement Serializable or Externalizable can be serialized; otherwise an exception is thrown.

Serialization methods for a sample User class

If User implements only Serializable, default serialization is used.

If User implements Serializable and defines custom writeObject / readObject, those methods are invoked.

If User implements Externalizable, the class must provide writeExternal and readExternal implementations.

Serialization steps

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\object.out"));
oos.writeObject(new User("xuliugen", "123456", "male"));

Deserialization steps

ObjectInputStream ois = new ObjectInputStream(new FileInputStream("object.out"));
User user = (User) ois.readObject();

It is crucial that the order of writes matches the order of reads.

Example diagrams

3. Important Considerations

Only the object's state is serialized; methods are not.

If a superclass implements Serializable, subclasses inherit serialization automatically.

Referenced objects are also serialized recursively.

Not all objects are serializable (e.g., those holding sockets, threads, or security‑sensitive fields).

Static and transient fields are excluded from serialization.

A serialVersionUID version identifier ensures compatibility between serialized forms and class definitions.

Many core Java classes (e.g., String, Vector) are already serializable; some (e.g., Hashtable) are not.

Serialization can be used for deep copying when an object's fields are themselves objects.

4. Summary

Java provides built‑in serialization mechanisms via ObjectOutputStream and ObjectInputStream, allowing objects to be persisted or transmitted. Understanding the underlying process, required interfaces, custom methods, and pitfalls such as non‑serializable fields or versioning is essential for reliable use.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendJavaserializationDeserializationObjectInputStreamObjectOutputStreamJava IO
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.