Nothing in your code example is directly using UTF-8. Java strings are encoded in memory using UTF-16 instead. Unicode codepoints that do not fit in a single 16-bit char will be encoded using a 2-char pair known as a surrogate pair.
If you do not pass a parameter value to String.getBytes()
, it returns a byte array that has the String
contents encoded using the underlying OS's default charset. If you want to ensure a UTF-8 encoded array then you need to use getBytes("UTF-8")
instead.
Calling String.charAt()
returns an original UTF-16 encoded char from the String's in-memory storage only.
So in your example, the Unicode character ?
is stored in the String
in-memory storage using two bytes that are UTF-16 encoded (0x6E 0xFF
or 0xFF 0x6E
depending on endian), but is stored in the byte array from getBytes()
using three bytes that are encoded using whatever the OS default charset is.
In UTF-8, that particular Unicode character happens to use 3 bytes as well (0xEF 0xBD 0xAE
).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…