The Java Serialization File Format: Handles, Class Descriptors and Fingerprints
Read a Java serialization stream byte by byte: the AC ED magic number, TC_ type codes, class descriptors with serialVersionUID fingerprints and flags, field type codes, object handles and back-references, null markers, block data, and what ObjectStreamClass reports.
kvi5hnuCode Sprout team member11 min readOn this page
You never need to write serialization bytes by hand. ObjectOutputStream does that. Still, knowing the layout pays off. It explains how shared references survive a round trip, why a renamed field can break old files, and what the strange hex strings in a security advisory mean. This article takes one small stream apart byte by byte. The format is documented in full in the Java Object Serialization Specification, chapter 6, "Object Serialization Stream Protocol". The constant names used below (TC_OBJECT, SC_SERIALIZABLE and so on) come from java.io.ObjectStreamConstants.
The stream header
Every serialization stream starts with four bytes:
AC ED: the magic number (STREAM_MAGIC). Tools such asfilerecognize serialized data by it. In Base64 the header becomesrO0AB, which is a common sight in cookies and HTTP parameters, and a red flag in security audits.00 05: the stream protocol version (STREAM_VERSION). Version 2 of the protocol, which writes externalizable data in block-data mode, has been the default since JDK 1.2. The version number in the header itself has been 5 for decades.
After the header comes a sequence of contents: objects, strings, arrays, class descriptors, nulls, back-references, and blocks of raw data written by writeInt and friends. Each item starts with a one-byte type code.
Type codes
| Byte | Constant | Meaning |
|---|---|---|
70 | TC_NULL | a null reference |
71 | TC_REFERENCE | a reference to something already written, followed by a 4-byte handle |
72 | TC_CLASSDESC | a new class descriptor |
73 | TC_OBJECT | a new object |
74 | TC_STRING | a new string, 2-byte length plus modified UTF-8 |
75 | TC_ARRAY | a new array |
76 | TC_CLASS | a Class object |
77 | TC_BLOCKDATA | raw data, 1-byte length |
78 | TC_ENDBLOCKDATA | end of optional data for an object or class |
79 | TC_RESET | the writer called reset(), so all handles are forgotten |
7A | TC_BLOCKDATALONG | raw data, 4-byte length |
7B | TC_EXCEPTION | an exception occurred while writing |
7C | TC_LONGSTRING | a string longer than 65535 bytes, 8-byte length |
7D | TC_PROXYCLASSDESC | descriptor of a dynamic proxy class |
7E | TC_ENUM | an enum constant, written as its class descriptor plus its name |
Strings get their own code because they're so common. Their characters are stored in the same "modified UTF-8" used by DataOutput.writeUTF (article 22), so a string is 74, a two-byte length, and the bytes.
Class descriptors
Before the first object of a class can be written, the reader needs to know what the class looks like. The stream therefore carries a class descriptor:
72(TC_CLASSDESC)- the class name as a two-byte length plus modified UTF-8 (the binary name, such as
com.example.ItemorSolution$Item) - the 8-byte serial version UID
- one byte of flags
- a two-byte count of serializable fields, then one field descriptor per field
- the class annotation, normally empty, terminated by
78(TC_ENDBLOCKDATA) - the descriptor of the superclass if it is serializable, or
70(TC_NULL)
The flag byte combines these bits:
| Bit | Constant | Set when |
|---|---|---|
01 | SC_WRITE_METHOD | the class has its own writeObject method (article 34) |
02 | SC_SERIALIZABLE | the class implements Serializable |
04 | SC_EXTERNALIZABLE | the class implements Externalizable |
08 | SC_BLOCK_DATA | externalizable data is written in block-data mode (protocol version 2) |
10 | SC_ENUM | the class is an enum type |
A field descriptor is a one-character type code, the field name, and for object and array fields the field's type as a JVM type descriptor string:
| Code | Type | Code | Type |
|---|---|---|---|
B | byte | J | long |
C | char | S | short |
D | double | Z | boolean |
F | float | L | an object type, such as Ljava/lang/String; |
I | int | [ | an array type, such as [I or [Ljava/lang/String; |
Primitive fields are listed first, sorted by name, and then object fields, also sorted by name. That's why a field's position in your source file has no effect on the stream, while its name and type do.
The fingerprint
The 8 bytes after the class name identify the version of the class. If you declare
@Serial
private static final long serialVersionUID = 42L;
the stream simply holds 42. If you don't, the serialization machinery computes a value: it feeds the class name, modifiers, interfaces, fields, constructors and methods (in a fixed canonical order) into SHA-1 and takes the first 8 bytes of the hash. That value is the class's fingerprint. Any change to that information, even adding a harmless private method, changes the fingerprint. When the reader's class has a different UID from the one in the stream, readObject throws InvalidClassException. Article 35 shows how to keep files readable across versions.
Records and enums are special. A record's serial version UID is 0 unless it declares one, and the match isn't checked, because records are reconstructed through their canonical constructor from named components. An enum constant is written as just its class descriptor and its name, and enums always have a UID of 0.
Objects, handles and back-references
An ordinary object is 73 (TC_OBJECT), then its class descriptor (new, or a back-reference to one already written), then its field values. Values come class by class, from the topmost serializable superclass down to the actual class. Within each class, they appear in the order of the field descriptors: primitives as raw big-endian bytes, object fields as nested contents.
Every new item that can be referred to again (class descriptors, objects, strings, arrays, enum constants) receives a handle, which is a serial number. The first handle is 0x7E0000 (baseWireHandle), and each new item gets the next one. When the writer meets an object it has already written, it emits 71 (TC_REFERENCE) followed by the four-byte handle instead of a second copy. This is the mechanism behind the "serial numbers" that preserve shared references, as described in article 32.
Arrays are 75 (TC_ARRAY), a class descriptor for the array type (names like [I), a four-byte length, and the elements.
Block data
When a class writes extra data with writeInt, writeUTF and similar methods, either in a custom writeObject method or directly on the ObjectOutputStream, the bytes are wrapped in block-data records: 77 plus a one-byte length for blocks up to 255 bytes, or 7A plus a four-byte length. A custom writeObject method's data for an object ends with 78. This framing lets a reader skip data it doesn't understand.
Decoding a real stream
The program serializes one Item object twice, and then a null. It then walks the bytes and labels each piece. Finally it asks ObjectStreamClass, the runtime representation of a class descriptor, for the same information.
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.*;
public class Solution {
static class Item implements Serializable {
@Serial
private static final long serialVersionUID = 42L;
int count;
String label;
Item(int count, String label) {
this.count = count;
this.label = label;
}
}
@SuppressWarnings("serial") // no serialVersionUID on purpose
static class Unversioned implements Serializable {
int x;
}
static ByteBuffer in;
static int handle = 0x7E0000;
static String hex(int n) {
var sb = new StringBuilder();
for (int i = 0; i < n; i++) sb.append(String.format("%02X ", in.get()));
return sb.toString().trim();
}
static String utf(String what) {
int len = in.getShort(in.position());
String raw = hex(2);
var bytes = new byte[len];
in.get(bytes);
return raw + " \"" + new String(bytes, StandardCharsets.UTF_8) + "\" " + what;
}
static void line(String bytes, String comment) {
System.out.printf("%-34s %s%n", bytes, comment);
}
public static void main(String[] args) throws IOException {
var item = new Item(3, "box");
var bytes = new ByteArrayOutputStream();
try (var out = new ObjectOutputStream(bytes)) {
out.writeObject(item);
out.writeObject(item); // the same object again
out.writeObject(null);
}
byte[] data = bytes.toByteArray();
System.out.println(data.length + " bytes:");
System.out.println(HexFormat.ofDelimiter(" ").withUpperCase().formatHex(data));
System.out.println();
in = ByteBuffer.wrap(data);
line(hex(2), "magic number");
line(hex(2), "stream format version 5");
line(hex(1), "TC_OBJECT: a new object");
line(hex(1), " TC_CLASSDESC: a new class descriptor, handle " + Integer.toHexString(handle++));
System.out.println(" " + utf("class name"));
line(" " + hex(8), "serialVersionUID = " + in.getLong(in.position() - 8));
line(" " + hex(1), "flags: SC_SERIALIZABLE");
int fields = in.getShort(in.position());
line(" " + hex(2), fields + " field descriptors");
for (int i = 0; i < fields; i++) {
char type = (char) in.get(in.position());
String code = hex(1);
System.out.println(" " + code + " '" + type + "' " + utf("field name"));
if (type == 'L' || type == '[') {
String tc = hex(1);
System.out.println(" " + tc + " TC_STRING " + utf("field type, handle " + Integer.toHexString(handle++)));
}
}
line(" " + hex(1), "TC_ENDBLOCKDATA: end of class annotations");
line(" " + hex(1), "TC_NULL: no serializable superclass");
System.out.println(" object handle " + Integer.toHexString(handle++) + ", field values:");
line(" " + hex(4), "count = 3 (primitive fields first)");
String tc = hex(1);
System.out.println(" " + tc + " TC_STRING " + utf("label, handle " + Integer.toHexString(handle++)));
line(hex(1), "TC_REFERENCE: second writeObject of the same item");
line(hex(4), " handle " + Integer.toHexString(in.getInt(in.position() - 4)));
line(hex(1), "TC_NULL: writeObject(null)");
System.out.println("bytes left: " + in.remaining());
System.out.println();
// class descriptors and fingerprints
ObjectStreamClass desc = ObjectStreamClass.lookup(Item.class);
System.out.println("descriptor: " + desc.getName() + ", UID " + desc.getSerialVersionUID()
+ ", fields " + Arrays.stream(desc.getFields()).map(f -> f.getTypeCode() + " " + f.getName()).toList());
long computed = ObjectStreamClass.lookup(Unversioned.class).getSerialVersionUID();
System.out.println("Unversioned has a computed fingerprint: " + (computed != 0));
record Point(int x, int y) implements Serializable { }
System.out.println("record without serialVersionUID: " + ObjectStreamClass.lookup(Point.class).getSerialVersionUID());
System.out.println("String descriptor fields: " + ObjectStreamClass.lookup(String.class).getFields().length);
}
}
87 bytes:
AC ED 00 05 73 72 00 0D 53 6F 6C 75 74 69 6F 6E 24 49 74 65 6D 00 00 00 00 00 00 00 2A 02 00 02 49 00 05 63 6F 75 6E 74 4C 00 05 6C 61 62 65 6C 74 00 12 4C 6A 61 76 61 2F 6C 61 6E 67 2F 53 74 72 69 6E 67 3B 78 70 00 00 00 03 74 00 03 62 6F 78 71 00 7E 00 02 70
AC ED magic number
00 05 stream format version 5
73 TC_OBJECT: a new object
72 TC_CLASSDESC: a new class descriptor, handle 7e0000
00 0D "Solution$Item" class name
00 00 00 00 00 00 00 2A serialVersionUID = 42
02 flags: SC_SERIALIZABLE
00 02 2 field descriptors
49 'I' 00 05 "count" field name
4C 'L' 00 05 "label" field name
74 TC_STRING 00 12 "Ljava/lang/String;" field type, handle 7e0001
78 TC_ENDBLOCKDATA: end of class annotations
70 TC_NULL: no serializable superclass
object handle 7e0002, field values:
00 00 00 03 count = 3 (primitive fields first)
74 TC_STRING 00 03 "box" label, handle 7e0003
71 TC_REFERENCE: second writeObject of the same item
00 7E 00 02 handle 7e0002
70 TC_NULL: writeObject(null)
bytes left: 0
descriptor: Solution$Item, UID 42, fields [I count, L label]
Unversioned has a computed fingerprint: true
record without serialVersionUID: 0
String descriptor fields: 0
Read the output against the rules above:
- The handles go
7e0000for the class descriptor,7e0001for the type stringLjava/lang/String;,7e0002for the object, and7e0003for the string"box". Strings inside descriptors are ordinary strings, so they get handles too. - The second
writeObject(item)costs five bytes:71 00 7E 00 02. - The class name is
Solution$Item, the binary name of a nested class. The$separator is what the reader passes toClass.forName. - The class without a declared UID still has a nonzero fingerprint. The local record reports 0.
Stringhas its own wire format (TC_STRING), so its descriptor lists no serializable fields.
Common questions
Which value should I choose for serialVersionUID: 1L or a long generated number?
Either works; the value only has to stay the same across compatible versions of the class. 1L is common and readable. Generating the value with serialver matters only when you must stay compatible with streams written by an old version that had no declared UID. Change the value deliberately when you make an incompatible change, so old data fails clearly instead of loading wrongly.
Does reordering fields in my class break old serialized data? No. Fields are matched by name, and the stream sorts them, so source order is irrelevant. Renaming a field or changing its type does matter.
How do I recognize serialized Java objects in logs, cookies or network traffic?
Look for the bytes AC ED 00 05 at the start, or the Base64 prefix rO0AB. Seeing either in data that crosses a trust boundary should prompt a security review, as explained in article 35.
Why does String get special treatment?
Strings are so common that a compact representation saves a lot of space: a type code, a length and the characters, with no class descriptor. Strings longer than 65,535 bytes in modified UTF-8 use TC_LONGSTRING with an 8-byte length.
Do I need to know this format to use serialization? No. It helps when debugging compatibility problems, reading security advisories, or analyzing a stream you received, but everyday code never touches the bytes.
API reference
| API | Purpose |
|---|---|
ObjectStreamConstants | constants for magic, version, TC_* codes, SC_* flags and baseWireHandle |
ObjectStreamClass.lookup(Class) | descriptor of a serializable class, or null if it isn't serializable |
ObjectStreamClass.lookupAny(Class) | descriptor for any class, serializable or not |
ObjectStreamClass.getName() | binary name stored in the stream |
ObjectStreamClass.getSerialVersionUID() | declared or computed UID |
ObjectStreamClass.getFields() | serializable fields as ObjectStreamField[], in stream order |
ObjectStreamField.getTypeCode() | 'B', 'C', 'D', 'F', 'I', 'J', 'S', 'Z', 'L' or '[' |
ObjectStreamField.getTypeString() | JVM type descriptor for object fields, null for primitives |
ObjectOutputStream.reset() | forget all handles; later writes start fresh copies |
HexFormat.ofDelimiter(" ").formatHex(bytes) | quick hex dump for inspecting a stream |
Summary
- A serialization stream starts with
AC ED 00 05, then a sequence of items, each introduced by a one-byteTC_code. - A class descriptor holds the class name, an 8-byte serial version UID, flags, field descriptors (primitives first, sorted by name), an annotation ending in
78, and the superclass descriptor or70. - Without a declared
serialVersionUID, the UID is a SHA-1-based fingerprint of the class's structure, and it changes when almost anything changes. Declare it explicitly. - Every object, string, array and descriptor gets a handle starting at
0x7E0000. Repeats are written as71plus the handle, which preserves shared references and cycles. - Primitive data written by custom code travels in block-data records (
77or7A). ObjectStreamClassandObjectStreamFieldexpose the same descriptor information at run time.
Practice Questions
What are the first four bytes of every serialization stream, and what does their Base64 form look like?
AC ED 00 05: the magic number ACED and protocol version 5. In Base64 a stream starts with rO0AB, which is why that prefix in a cookie or request parameter is a sign that a server deserializes client data.
Why does the serialized form of a second reference to the same object take only five bytes?
The writer remembers every object it has written, with its handle. The second time it sees the same object, it writes TC_REFERENCE (71) and the four-byte handle instead of the object's contents. The reader maps the handle back to the object it already rebuilt, so both references point to one object again.
You reorder two fields in a class's source file. Does the stream format change? What if you rename one?
Reordering doesn't matter: field descriptors are sorted, primitives first, then objects, each group by name. Renaming a field does matter. The stream stores field names, so an old stream's label value won't be assigned to a field now called title. And if the class has no explicit serialVersionUID, any of these edits can change the computed fingerprint and make old streams fail with InvalidClassException.
:::details[What do the type codes I, J, Z and [ mean in a field descriptor?]
I is int, J is long, Z is boolean, and [ is an array type, followed by a descriptor string such as [I or [Ljava/lang/String;. These are the JVM's own type descriptors, the same ones you see in javap output.
:::
Why does the specification discourage serializing inner classes and lambdas?
The compiler generates synthetic names, fields and methods for them, and these details aren't fixed by the language specification. They feed into the class name in the stream and into the computed fingerprint, so a stream written by one build may not be readable by another, even with no source changes.
Previous: Java Object Serialization: Saving and Loading Objects
Next: Customizing Java Serialization: transient, readObject, writeObject and Externalizable
Related reading
- Java Reflection and Generics: Class<T>, Type Tokens and the Type Interface
- Java Generics Inheritance Rules and Wildcards: extends, super and ?
- Java Generics: Generic Classes and Generic Methods
- Throwing Exceptions and Creating Custom Exception Classes in Java
- Java Local, Anonymous and Static Nested Classes

More in Core Java II
FFM Upcalls, Variadic Functions and errno
Advanced FFM calls: passing Java code to C as function pointers with upcall stubs, binding objects and lambdas, calling variadic functions such as printf, capturing errno and GetLastError, and a C library that calls back into Java. The final article of the course.
Calling Foreign Functions: Linker, Symbol Lookup, Downcalls and jextract
Calling C functions from pure Java with the FFM API: the native linker, default, library and loader symbol lookups, function descriptors, downcall handles, invokeExact, pointers and NULL, struct results, critical calls, enabling native access, and generating bindings with jextract.
FFM Memory Layouts: Structs, Unions, Sequences and Var Handles
Describing C data with FFM memory layouts: value layouts and C types, struct and union layouts, alignment and padding, sequences, layout paths, var handles with base offsets, offset and slice handles, pointers with target layouts, and atomic access modes.
Foreign Function and Memory API: Arenas and Memory Segments
Getting started with the FFM API (final since Java 22): a first foreign call, confined, shared, automatic and global arenas, native and heap memory segments, reading and writing values, C strings and arrays, alignment and byte order, slices, copies, allocators, and memory-mapped files.
Discussion
Sign in to ask a question or share what you learned.