JVM Knowledge

The CONSTANT_Utf8_info structure is used to represent constant string values:

CONSTANT_Utf8_info {
    u1 tag;
    u2 length;
    u1 bytes[length];
}

The items of the CONSTANT_Utf8_info structure are as follows:

  • tag

    The tag item has the value CONSTANT_Utf8 (1).

  • length

    The value of the length item gives the number of bytes in the bytes array (not the length of the resulting string).

  • bytes[]

    The bytes array contains the bytes of the string.

    No byte may have the value (byte)0.

    No byte may lie in the range (byte)0xf0 to (byte)0xff.

String content is encoded in modified UTF-8. Modified UTF-8 strings are encoded so that code point sequences that contain only non-null ASCII characters can be represented using only 1 byte per code point, but all code points in the Unicode codespace can be represented. Modified UTF-8 strings are not null-terminated. The encoding is as follows:

  • Code points in the range '\u0001' to '\u007F' are represented by a single byte:

    Table 4.7.

    0 bits 6-0

    The 7 bits of data in the byte give the value of the code point represented.

  • The null code point ('\u0000') and code points in the range '\u0080' to '\u07FF' are represented by a pair of bytes x and y :

    Table 4.8.

    x:

    Table 4.9.

    1 1 0 bits 10-6

    y:

    Table 4.10.

    1 0 bits 5-0

    The two bytes represent the code point with the value:

    ((x & 0x1f) << 6) + (y & 0x3f)
    
  • Code points in the range '\u0800' to '\uFFFF' are represented by 3 bytes x, y, and z :

    Table 4.11.

    x:

    Table 4.12.

    1 1 1 0 bits 15-12

    y:

    Table 4.13.

    1 0 bits 11-6

    z:

    Table 4.14.

    1 0 bits 5-0

    The three bytes represent the code point with the value:

    ((x & 0xf) << 12) + ((y & 0x3f) << 6) + (z & 0x3f)
    
  • Characters with code points above U+FFFF (so-called supplementary characters) are represented by separately encoding the two surrogate code units of their UTF-16 representation. Each of the surrogate code units is represented by three bytes. This means supplementary characters are represented by six bytes, u, v, w, x, y, and z :

    Table 4.15.

    u:

    Table 4.16.

    1 1 1 0 1 1 0 1

    v:

    Table 4.17.

    1 0 1 0 (bits 20-16)-1

    w:

    Table 4.18.

    1 0 bits 15-10

    x:

    Table 4.19.

    1 1 1 0 1 1 0 1

    y:

    Table 4.20.

    1 0 1 1 bits 9-6

    z:

    Table 4.21.

    1 0 bits 5-0

    The six bytes represent the code point with the value:

    0x10000 + ((v & 0x0f) << 16) + ((w & 0x3f) << 10) +
    ((y & 0x0f) << 6) + (z & 0x3f)
    

The bytes of multibyte characters are stored in the class file in big-endian (high byte first) order.

There are two differences between this format and the "standard" UTF-8 format. First, the null character (char)0 is encoded using the 2-byte format rather than the 1-byte format, so that modified UTF-8 strings never have embedded nulls. Second, only the 1-byte, 2-byte, and 3-byte formats of standard UTF-8 are used. The Java Virtual Machine does not recognize the four-byte format of standard UTF-8; it uses its own two-times-three-byte format instead.

For more information regarding the standard UTF-8 format, see Section 3.9 Unicode Encoding Forms of The Unicode Standard, Version 15.0.