【发布时间】:2025-12-15 14:10:01
【问题描述】:
使用 new 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));
bytes2使用new String(byte[],"UTF-8"),结果(str2)在jdk7和jdk8不一样, 但 byte1 是一样的。 bytes2 有什么特别之处?
测试“ISO-8859-1”代码,bytes2的结果在jdk1.8中是一样的!
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]
【问题讨论】:
-
在 Java 10 中执行此操作时,您会得到与在 Java 8 中相同的结果。