CONSTANT_String tag 8

A java.lang.String literal.

Tag
8
Since
class file 45.0 (Java 1.0)
Size
3 bytes
Loadable
yes, with ldc, ldc_w
Referred to by
  • ldc and ldc_w
  • ConstantValue of String fields
  • bootstrap method arguments

Layout

TypeFieldWhat it holds
u1tag8
u2string_indexa Utf8 with the string's contents

What it is for

A java.lang.String literal. ldc of it pushes an interned String: every class that uses the same literal gets the same object, which is why "a" == "a" is true.

The String entry only points at the Utf8 that holds the text. The same Utf8 can also serve as a name elsewhere in the file.

Compile-time constant expressions are folded first: "limit" + " exceeded" with constant operands becomes one String constant.

How common it is

JDK 25.0.5's java.base has 59,564 String constants in 3,477 of its 7,391 classes: 5.6% of all 1,059,609 constants.

A real one, byte by byte

Constant #7 of Hello.class, compiled by javac 25:

OffsetBytesFieldValue
573 bytes#7 Stringworld
5708tag8 (String)
5800 08string_index#8

Open in the viewer

Specification: JVMS §4.4, CONSTANT_String_info (Java SE 25).