理解Java字符编码

时间:2014-09-05 15:07:12

标签: java encoding character-encoding nio

请帮助我理解以下代码行的输出:

byte[] byteArray1 = Charset.forName("UTF-8").encode("hello wor").array();
System.out.println(byteArray1.length); // 9 - This is as expected.

byte[] byteArray2 = Charset.forName("UTF-8").encode("hello world").array();
System.out.println(byteArray2.length); // 12 - Shouldn't it be 11?

byte[] byteArray3 = Charset.forName("UTF-16").encode("hello wor").array();
System.out.println(byteArray3.length); // 37 - Why?

byte[] byteArray4 = Charset.forName("UTF-16").encode("hello world").array();
System.out.println(byteArray4.length); // 45 - Why?

0 个答案:

没有答案