使用新的String(byte[], "UTF-8")创建的一些字节数组在jdk 1.7和1.8中返回不同的结果。
byte[] bytes1 = {55, 93, 97, -13, 4, 8, 29, 26, -68, -4, -26, -94, -37, 32, -41, 88};
String str1 = new String(bytes1,"UTF-8");
System.out.println(str1.length());
byte[] out1 = str1.getBytes("UTF-8");
System.out.println(out1.length);
System.out.println(Arrays.toString(out1));
byte[] bytes2 = {65, -103, -103, 73, 32, 68, 49, 73, -1, -30, -1, -103, -92, 11, -32, -30};
String str2 = new String(bytes2,"UTF-8");
System.out.println(str2.length());
byte[] out2 = str2.getBytes("UTF-8");
System.out.println(out2.length);
System.out.println(Arrays.toString(out2));
在使用new String(byte[],"UTF-8")方式将字节数组转为字符串时,jdk7和jdk8中结果(str2)不相同,但byte1却一样。bytes2有什么特殊之处?
测试"ISO-8859-1"编码,结果发现在jdk1.8中bytes2的结果是相同的!
jdk1.7.0_80:
15
27
[55, 93, 97, -17, -65, -67, 4, 8, 29, 26, -17, -65, -67, -17, -65, -67, -17, -65, -67, -17, -65, -67, 32, -17, -65, -67, 88]
15
31
[65, -17, -65, -67, -17, -65, -67, 73, 32, 68, 49, 73, -17, -65, -67, -17, -65, -67, -17, -65, -67, -17, -65, -67, -17, -65, -67, 11, -17, -65, -67]
jdk1.8.0_201
15
27
[55, 93, 97, -17, -65, -67, 4, 8, 29, 26, -17, -65, -67, -17, -65, -67, -17, -65, -67, -17, -65, -67, 32, -17, -65, -67, 88]
16
34
[65, -17, -65, -67, -17, -65, -67, 73, 32, 68, 49, 73, -17, -65, -67, -17, -65, -67, -17, -65, -67, -17, -65, -67, -17, -65, -67, 11, -17, -65, -67, -17, -65, -67]