As far as I know char in java is built from 2 bytes, so why this test passes?
assertEquals(4, "test".getBytes(Charset.forName("UTF-8")).length);
As far as I know char in java is built from 2 bytes, so why this test passes?
assertEquals(4, "test".getBytes(Charset.forName("UTF-8")).length);
In UTF-8, the char's in the range 0x00 to 0x7F are only 1 byte. Thus, it's 4 bytes long.
"test".getBytes(Charset.forName("UTF-16")
..would return 8 bytes.
EDIT: Added @Rossums comment for more detail.