小编sap*_*ing的帖子

新的String(byte [])结果在JDK 7和8中有所不同

某些使用新字符串的字节数组(字节[],“ 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));
Run Code Online (Sandbox Code Playgroud)

bytes2使用新的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, …
Run Code Online (Sandbox Code Playgroud)

java java-7 java-8

5
推荐指数
1
解决办法
108
查看次数

标签 统计

java ×1

java-7 ×1

java-8 ×1