This page follows chapter 6,
“Object
Serialization Stream Protocol”, of Oracle’s Java Object Serialization
Specification (Java SE 21), read 2026-11-12, with the constants in OpenJDK’s
java.io.ObjectStreamConstants,
read 2026-11-12, which match the chapter. Primitive sizes and byte order follow
OpenJDK’s
java.io.DataOutput,
and field order follows
ObjectStreamField
and ObjectStreamClass,
all read 2026-11-12. Numbers are big-endian (high-order byte first), as the chapter’s
example stream shows: the int 17 is 00 00 00 11.
Stream
AC ED (STREAM_MAGIC), 00 05
(STREAM_VERSION), then contents: objects and blocks of primitive data,
in the order the program wrote them. Each object starts with a type code.
Type codes
| Code | Name | Followed by |
|---|---|---|
70 | TC_NULL | Nothing: a null reference. |
71 | TC_REFERENCE | A 4-byte handle of an object already in the stream. |
72 | TC_CLASSDESC | A class descriptor. |
73 | TC_OBJECT | Class descriptor, then the object’s data for each class from the top superclass down. |
74 | TC_STRING | 2-byte unsigned length and modified UTF-8 (for strings under 65536 bytes). |
75 | TC_ARRAY | Class descriptor, 4-byte size, the elements. |
76 | TC_CLASS | Class descriptor: a Class object. |
77 | TC_BLOCKDATA | 1-byte size and that much primitive data. |
78 | TC_ENDBLOCKDATA | End of optional data written by writeObject or an annotation. |
79 | TC_RESET | Forget all known objects; handles restart at 0x7E0000. |
7A | TC_BLOCKDATALONG | 4-byte size and the data. |
7B | TC_EXCEPTION | A Throwable object, with a reset before and after it so that its objects do not overlap those sent before or after the exception. |
7C | TC_LONGSTRING | 8-byte signed length and modified UTF-8, for strings of 65536 bytes or more (since Java 2 v1.3). |
7D | TC_PROXYCLASSDESC | Dynamic proxy class: a 4-byte interface count, the interface names (each 2-byte length and modified UTF-8), annotation, superclass descriptor (java.lang.reflect.Proxy). |
7E | TC_ENUM | Class descriptor, then the constant’s name as a string object. |
Each new object, string, class, descriptor, array and enum gets a handle: the first
is 0x7E0000 (baseWireHandle) and they count up. A
descriptor’s handle is assigned after its class name and serialVersionUID.
Class descriptors
| Item | Encoding |
|---|---|
| className | 2-byte length and modified UTF-8, such as List in the specification’s example. |
| serialVersionUID | 8 bytes (a long), the Stream Unique Identifier. |
| flags | 1 byte, below. |
| fields | 2-byte count, then each: a type code byte, the name (2-byte length and modified UTF-8), and for object and array fields the type in field descriptor format as a string object (such as Ljava/lang/Object;). |
| classAnnotation | Contents written by annotateClass, then TC_ENDBLOCKDATA. |
| superClassDesc | The superclass’s descriptor, or TC_NULL if the superclass is not serialisable. |
| Flag | Name | Meaning |
|---|---|---|
01 | SC_WRITE_METHOD | The class has writeObject: its data is followed by optional data ending in TC_ENDBLOCKDATA. |
02 | SC_SERIALIZABLE | Serializable. |
04 | SC_EXTERNALIZABLE | Externalizable: the class writes all of its data itself. |
08 | SC_BLOCK_DATA | Externalizable data in block-data mode (protocol version 2, the default since JDK 1.2). |
10 | SC_ENUM | An enum type. |
Field type codes
| Code | Type | Size |
|---|---|---|
B | byte | 1 |
C | char | 2 |
D | double | 8 |
F | float | 4 |
I | int | 4 |
J | long | 8 |
S | short | 2 |
Z | boolean | 1 |
[ | array | An object |
L | object | An object |
A class’s field values are written in the order of its descriptor. OpenJDK sorts
the descriptor’s fields with primitive fields before object fields, and by name
within each group, so the primitive values come first. A boolean is one byte,
01 or 00; char and short two bytes; float four bytes, as
Float.floatToIntBits.
The full grammar, annotations and special cases
Section 6.4 of the specification (read 2026-11-13). Notation: (utf) is a
2-byte length string, (long-utf) an 8-byte length one; newHandle
takes the next handle number; <n> a count read from the stream.
stream: magic version contents
contents: content | contents content
content: object | blockdata
object: newObject | newClass | newArray | newString | newEnum
| newClassDesc | prevObject | nullReference | exception | TC_RESET
newClass: TC_CLASS classDesc newHandle
classDesc: newClassDesc | nullReference | (ClassDesc)prevObject
newClassDesc: TC_CLASSDESC className serialVersionUID newHandle classDescInfo
| TC_PROXYCLASSDESC newHandle proxyClassDescInfo
classDescInfo: classDescFlags fields classAnnotation superClassDesc
proxyClassDescInfo: (int)<count> proxyInterfaceName[count] classAnnotation superClassDesc
fields: (short)<count> fieldDesc[count]
fieldDesc: primitiveDesc | objectDesc
primitiveDesc: prim_typecode fieldName
objectDesc: obj_typecode fieldName className1 (className1: a String object, field descriptor format)
classAnnotation: endBlockData | contents endBlockData (contents written by annotateClass)
newArray: TC_ARRAY classDesc newHandle (int)<size> values[size]
newObject: TC_OBJECT classDesc newHandle classdata[]
classdata: nowrclass (SC_SERIALIZABLE, no SC_WRITE_METHOD)
| wrclass objectAnnotation (SC_SERIALIZABLE and SC_WRITE_METHOD)
| externalContents (SC_EXTERNALIZABLE, no SC_BLOCK_DATA)
| objectAnnotation (SC_EXTERNALIZABLE and SC_BLOCK_DATA)
objectAnnotation: endBlockData | contents endBlockData (written by writeObject or, in protocol 2, writeExternal)
blockdata: TC_BLOCKDATA (unsigned byte)<size> bytes | TC_BLOCKDATALONG (int)<size> bytes
externalContent: (bytes) | object (only parseable by readExternal)
externalContents: externalContent | externalContents externalContent (protocol version 1)
newString: TC_STRING newHandle (utf) | TC_LONGSTRING newHandle (long-utf)
newEnum: TC_ENUM classDesc newHandle enumConstantName (a String object)
prevObject: TC_REFERENCE (int)handle
exception: TC_EXCEPTION reset (Throwable)object reset
- Per-class data. A new object's data is written for each serializable class, the highest superclass first: its serializable fields, then, if the class has
writeObject(SC_WRITE_METHOD), optional objects and block-data of primitives, ending withTC_ENDBLOCKDATA. All primitive data is buffered into block-data records, whether written insidewriteObjector directly outside it; an object written bywriteObjectends the block-data record before it and is a regular object, null or back reference. A reader may discard any data up toTC_ENDBLOCKDATA. - Block data in protocol version 2 is normalised to chunks of at most 1,024 bytes. Protocol version 1 (JDK 1.1's default) is the initial format; version 2 (the default since JDK 1.2) adds the external data format with
SC_BLOCK_DATAandTC_ENDBLOCKDATAfor externalizable data. JDK 1.1.7 and later read both. - Externalizable. The class writes everything itself with
writeExternal. In protocol 2 (SC_BLOCK_DATAset) the content is anobjectAnnotation: block data and objects, ending withTC_ENDBLOCKDATA. In protocol 1 it isexternalContentswith no end marker and no block framing, so only the class's ownreadExternalcan parse it. - classAnnotation is whatever
annotateClasswrote (often the code base URL), thenTC_ENDBLOCKDATA; for a proxy classannotateProxyClass. - Enum constants (
TC_ENUM): the class descriptor of the enum type (flagSC_ENUM, fixed serialVersionUID 0, no fields), a handle, then the constant'sname()as a String object (a back reference if the string appeared before). No field values; reading usesEnum.valueOf. CustomwriteObject,readObject,writeReplace,readResolve,serialPersistentFieldsandserialVersionUIDof an enum are ignored. - Class objects (
TC_CLASS): the descriptor of the class it names, then a handle. - Records use the ordinary
TC_OBJECTform: the descriptor of the record class, a handle, and the record components written as the serialisable fields. On reading, the fields are rebuilt and the canonical constructor is called (a missing component takes its type's default). The record's handle is assigned only after construction, so a component that refers back to its own record is not preserved.writeObject,readObject,readObjectNoData,writeExternal,readExternalandserialPersistentFieldsare ignored;writeReplaceandreadResolvework. The serialVersionUID is 0 unless declared, and matching is waived. - serialPersistentFields. By default the serial fields are the non-transient, non-static fields. A
private static final ObjectStreamField[] serialPersistentFieldslists the names and types instead; one that is null, the wrong type or without those modifiers is ignored. It frees the stream form from the class's real fields, withwriteObjectandreadObjectmapping between them, and cannot be used by inner classes (but can by static nested classes). - Order of writing an object (ObjectOutputStream):
writeReplace(not for enums), thenreplaceObjectif enabled, repeated on replacements; strings; arrays (descriptor, handle, length, elements); enum constants; records; then ordinary objects (descriptor, handle, class data).writeUnsharedwrites a fresh copy that later back references do not point to. An IOException while writing is itself written with the exception protocol. - Field descriptor format for
className1: the JVM type signature,Ljava/lang/String;for an object,[Ifor an int array.
putFields, readFields and readObjectNoData
From chapters 2 and 3 of the specification (Java SE 21, read 2026-11-13). None of these
changes the stream grammar; they are the APIs a class uses inside its own
writeObject and readObject.
defaultWriteObjectwrites all serializable fields of the current class (callable only fromwriteObject, elseNotActiveException).putFields/writeFields:putFieldsreturns aPutFieldobject in which fields are set in any order by name;writeFieldsthen writes them in canonical order; an unset field is written with the default value of its type. It must be called fromwriteObject, once, and not afterdefaultWriteObject; other data may be written only afterwriteFields. The stream format is the same as fordefaultWriteObject: the API only avoids the need for real non-transient fields matching each named serial field.defaultReadObject/readFields:readFieldsreads the serial fields into aGetFieldobject, valid only during thatreadObjectcall; fields are retrieved in any order by name and type (IllegalArgumentExceptionif name or type is not a serial field of the current class);defaulted(name)is true when the stream lacks the field and the default value is returned;getObjectStreamClass()gives the class as written in the stream with its field list. Exactly one ofdefaultReadObjectorreadFieldsmust be called, once, before reading optional data, even if no optional data is read. Fields not in the stream are set to defaults; stream fields not in the class are discarded. The serial fields are those ofserialPersistentFieldsor else the non-transient, non-static fields.- Reading past the optional data of a class returns −1 for byte reads, throws
EOFExceptionfor primitive reads andOptionalDataException(eof true) for object reads. readObjectNoData(private void readObjectNoData() throws ObjectStreamException) is called instead ofreadObjectfor a class that the receiver's class hierarchy has but the stream's class descriptor chain does not list (a different class version, or a tampered stream); it initialises that class's fields. Without it the fields keep their default values. It is called at the point at which that class'sreadObjectwould otherwise have run. Records ignore it.ObjectInputValidation:registerValidation(obj, priority)requestsvalidateObjectafter the whole graph is read; higher priorities first; a thrownInvalidObjectExceptionends validation.readExternalandwriteExternalare public, so a class may need to guard against being invoked on an existing object.writeClassDescriptor/readClassDescriptor(JDK 1.3 and later) can be overridden to change how class descriptors are written, as a pair; they are not used with the old protocol version 1 format.
Serialized forms of standard classes
From the Java SE 21 API documentation, “Serialized Form”
(docs.oracle.com/en/java/javase/21/docs/api/serialized-form.html, read 2026-11-13), which covers
every serializable class in the platform. The page below gives the classes met most often;
all others are on that page. For each class the stream holds a TC_OBJECT whose
class descriptor has the class name, the serialVersionUID shown, the flags
(SC_SERIALIZABLE, plus SC_WRITE_METHOD if the class has
writeObject) and the serialized fields in the order given by the rules of the
specification (primitive fields first, then object fields, each group sorted by name); the class data then holds the
field values, and for a class with writeObject the extra data listed as “extra
data” (primitives in block-data records, objects as ordinary objects) up to
TC_ENDBLOCKDATA.
| Class | serialVersionUID | Serialized fields | Extra data written by writeObject (“Serial Data”) |
|---|---|---|---|
java.lang.String | −6849794470754667710 | none: special-cased | Written as TC_STRING or TC_LONGSTRING (section 6.2), never as an object. |
java.lang.Class | 3206093459760846163 | none: special-cased | TC_CLASS then a class descriptor (the ObjectStreamClass form), with a new handle on first appearance. |
Boolean, Byte, Character, Short, Integer, Long, Float, Double | −3665804199014368530, −7183698231559129828, 3786198910865385080, 7515723908773894738, 1360826667806852920, 4290774380558885855, −2671257302660747028, −9172774392245257468 | one field value of the matching primitive type (boolean, byte, char, short, int, long, float, double) | none. They extend java.lang.Number (UID −8742448824652078965, no fields), which appears as the superclass descriptor with no data (Boolean and Character extend Object). |
java.math.BigInteger | −8287574255936472291 | int signum, byte[] magnitude (big-endian), and for compatibility int bitCount, int bitLength, int firstNonzeroByteNum, int lowestSetBit | The compatibility fields are written as −1, −1, −2 and −2 (bitCount, bitLength, lowestSetBit, firstNonzeroByteNum) and ignored when read; the magnitude is a byte array for historical reasons. |
java.math.BigDecimal | 6108874887143696463 | BigInteger intVal (the unscaled value), int scale | Class has writeObject, no documented extra data. readObjectNoData is unsupported. |
java.lang.StringBuffer, StringBuilder | 3388685877147921107, 4383685877147921099 | StringBuffer: int count, boolean shared (ignored on reading), char[] value. StringBuilder: see the Serialized Form page (it has readObject/writeObject). | — |
java.lang.StackTraceElement | 6992337162326171013 | classLoaderName, declaringClass, fileName, methodName, moduleName, moduleVersion (Strings), int lineNumber, byte format | none |
java.lang.Throwable (and exceptions) | −3042686055658047285 | Throwable cause (the throwable itself means “not yet initialised”), String detailMessage, StackTraceElement[] stackTrace | writeObject and readObject exist; a null stack trace is written as a one-element array whose element equals new StackTraceElement("", "", null, Integer.MIN_VALUE) (read back as null); suppressed exceptions are also stored (null entries and self references are rejected when reading). |
java.util.ArrayList | 8683452581122892189 | int size | The length of the backing array (int), then all elements in order, each an object. |
java.util.LinkedList | 876323262645176354 | none | The size (int), then all elements in order, each an object. |
java.util.ArrayDeque | 2340985798034038923 | none | The size (int), then all elements first to last, each an object. |
java.util.Vector | −2767605614048989439 | see the Serialized Form page (capacityIncrement, elementCount, elementData) | Written under synchronisation; readObject checks the fields for consistency. |
java.util.HashMap | 362498820763181265 | float loadFactor, int threshold | The bucket array length (int), the number of mappings (int), then key and value objects of each mapping, in no particular order. |
java.util.LinkedHashMap | 3801124242820219131 | boolean accessOrder (plus the HashMap data) | As HashMap. |
java.util.HashSet, LinkedHashSet | −5024744406713321676, −2851667679971038690 | none | HashSet: the backing map's capacity (int), its load factor (float), the size (int), then all elements in no particular order. LinkedHashSet adds nothing. |
java.util.Hashtable | 1421746759512286392 | float loadFactor, int threshold | Bucket array length (int), mapping count (int), then key and value of each mapping in no particular order. |
java.util.TreeMap | 919286545866124006 | Comparator comparator (null for natural order) | The size (int), then key and value of each mapping in key order. |
java.util.TreeSet | −2479143000061671589 | none | The comparator (object, null if natural order), the size (int), then all elements in order. |
java.util.PriorityQueue | −7720805057305804111 | Comparator comparator, int size | The length of the backing array (int), then the elements in array order. |
java.util.EnumMap | 458661240069192865 | Class keyType (the enum type) | The size (int), then key and value of each mapping. |
java.util.BitSet | 7997698588986878753 | long[] bits (bit i in bits[i/64], position i % 64, least significant bit first) | — |
java.util.Date | 7523967970034938905 | none | The value of getTime() as a long (milliseconds since 1970-01-01T00:00:00 GMT). |
java.util.UUID | −4856846361193249489 | long leastSigBits, long mostSigBits | none |
Immutable collections (List.of, Set.of, Map.of and similar) | java.util.CollSer: 6309168927139932177 | int tag: low 8 bits 1 immutable List, 2 Set, 3 Map, 4 List allowing nulls; the high 24 bits are zero when written and ignored when read | A non-negative int count, then that many objects: for a Map twice the mapping count, as key, value, key, value …. The immutable class itself is never in the stream; reading creates the collection as the static factory would. |
java.time classes (Duration, Instant, LocalDate and others) | each class has its own UID; the stream holds java.time.Ser (UID −7683839454370182990), Externalizable | The instance is replaced by Ser through writeReplace. | A type byte then the values; see the table below. |
| Type byte | Class | Values written after the byte |
|---|---|---|
| 1 | Duration | long seconds, int nanos |
| 2 | Instant | long seconds (from 1970-01-01T00:00:00Z), int nanos |
| 3 | LocalDate | int year, byte month, byte day |
| 4 | LocalTime | Bytes are cut short using ones' complement: if nano is 0 and second is 0 and minute is 0 then ~hour; if nano and second are 0 then hour, ~minute; if nano is 0 then hour, minute, ~second; otherwise hour, minute, second, int nano. A negative byte (the complement) marks the end. |
| 5 | LocalDateTime | the LocalDate then the LocalTime bodies, without their type bytes |
| 6 | ZonedDateTime | the LocalDateTime, ZoneOffset and ZoneId bodies, no type bytes |
| 7 | ZoneId (not an offset) | writeUTF(id) |
| 8 | ZoneOffset | byte offsetByte = totalSeconds / 900 when totalSeconds is a multiple of 900, else 127 followed by int totalSeconds |
| 9 | OffsetTime | the LocalTime and ZoneOffset bodies |
| 10 | OffsetDateTime | the LocalDateTime and ZoneOffset bodies |
| 11 | Year | int year |
| 12 | YearMonth | int year, byte month |
| 13 | MonthDay | byte month, byte day |
| 14 | Period | int years, int months, int days |
Reading maps the type to the factory: Duration.ofSeconds, Instant.ofEpochSecond, LocalDate.of, LocalTime.of, ZonedDateTime.ofLenient, ZoneId.of and so on. The java.time.chrono classes have their own Ser class with type bytes (1 Chronology as writeUTF(id), 2 ChronoLocalDateTime, 3 ChronoZonedDateTime, 4 JapaneseDate, 5 JapaneseEra, 6 HijrahDate, 8 MinguoDate, 10 ThaiBuddhistDate) as listed in the Serialized Form page, as do java.time.zone (ZoneRules, transitions). Classes of the platform not listed here (about 600, including the Collections wrappers, concurrent collections, atomics, text formats and exceptions) follow the same rules and are documented on the Serialized Form page; their layouts were not transcribed.
Example
AC ED 00 05 magic, version 5
74 00 05 68 65 6C 6C 6F TC_STRING "hello" (handle 7E0000)