The last line of code in theArrayCopyTest
example uses aString
constructor that creates aString
object from an array of bytes. This constructor did not convert correctly between bytes and characters and has been deprecated. TheString
class provides two alternative constructors: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.String(byte[]) or String(byte[], String)Here's a new version of this example using the constructor that uses the default character encoding:
For details about this and other changes to theclass 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)); } }String
class, see 1.1 Changes: String Class.