4
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public class Java87String {

public static void main(String[] args) throws UnsupportedEncodingException {
        // TODO Auto-generated method stub

        //byte[] b = {-101, 53, -51, -26, 24, 60, 20, -31, -6, 45, 50, 103, -66, 28, 114, -39, 92, 23, -47, 32, -5, -122, -28, 79, 22, -76, 116, -122, -54, -122};
        //byte[] b = {-76, -55, 85, -50, 80, -23, 27, 62, -94, -74, 47, -123, -119, 94, 90, 61, -63, 73, 56, -48, -54, -4, 11, 79};

        byte[] b = { -5, -122, -28};

        System.out.println("Input Array :" + Arrays.toString(b));
        System.out.println("Array Length : " + b.length);                       
        String target = new String(b,StandardCharsets.UTF_8);
        System.out.println(Arrays.toString(target.getBytes("UTF-8")));
        System.out.println("Final Key :" + target);

}
}

The above code returns the following output in Java 7

Input Array :[-5, -122, -28]
Array Length : 3
[-17, -65, -67]
Final Key :�

The Same code returns the following output in Java 8

Input Array :[-5, -122, -28]
Array Length : 3
[-17, -65, -67, -17, -65, -67, -17, -65, -67]
Final Key :���

Sounds like Java8 is doing the right thing of replacing with the default sequence of [-17, -65, -67].

Why is there a difference in output and Any Known bugs in JDK 1.7 which fixes this issue?

Alex Chermenin
  • 767
  • 9
  • 21
  • Possible duplicate of [Java 8 UTF-8 encoding issue (java bug?)](http://stackoverflow.com/questions/25404373/java-8-utf-8-encoding-issue-java-bug) – DVarga Sep 29 '16 at 09:28

2 Answers2

1

I think (-5, -122, -28) is a invalid UTF-8 byte sequence, so the JVM may output anything in this case. If it were a valid one, maybe the different Java versions could show the same output.

Does this specific byte sequence have a meaning? just curious

nandsito
  • 3,634
  • 2
  • 16
  • 25
  • It doesn't have any meaning to it. The other byte arrays commented in the code does behave differently and are other examples of this issue. – Meenakshi sundaram Sep 30 '16 at 06:55
1

Per the String JavaDoc:

The behavior of this constructor when the given bytes are not valid in the given charset is unspecified. The CharsetDecoder class should be used when more control over the decoding process is required.

javabrett
  • 5,507
  • 3
  • 42
  • 68