Previous


1.1 Changes: Array Copy Example

The last line of code in the ArrayCopyTest example uses a String constructor that creates a String object from an array of bytes. This constructor did not convert correctly between bytes and characters and has been deprecated. The String class provides two alternative constructors:
String(byte[])
   or
String(byte[], String)
The first constructor converts the byte array to characters using the default character encoding. The second converts the byte array to characters using the specified character encoding.

Here's a new version of this example using the constructor that uses the default character encoding:

class ArrayCopyTest {
    public static void main(String[] args) {
        byte[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e', 'i', 'n', 'a', 't', 'e', 'd' };
        byte[] copyTo = new byte[7];

        System.arraycopy(copyFrom, 2, copyTo, 0, 7);
        System.out.println(new String(copyTo));
    }
}
For details about this and other changes to the String class, see 1.1 Changes: String Class.


Previous