This means that you can create a new "empty" array of any length using this method, as described for the situation // Returns single element (0) array .
The main goal why the Arrays.copyOfRange method Arrays.copyOfRange is that it creates a new array on the fly. And this new array may be larger than the original array, of course.
This behavior is a documented function.
The end index of the range (to) , which must be greater than or equal to, can be greater than original.length , in which case '\ u000' is placed in all elements of the copy whose index is greater than or equal to the original .length - from. The length of the returned array will be - from.
Why is this implemented like this? Assuming you will reference this:
byte[] bytes = new byte[] {1, 1, 1}; byte[] newBytes = Arrays.copyOfRange(bytes, 2, 6);
It will create an array [1, 0, 0, 0], and all elements that do not exist in the original array will be initialized with a default literal for the current type. But , if you want to specify from more, but not equal bytes.length (which is discouraged by the documentation), this will ArrayIndexOutOfBoundsException while calling this System.arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength)); because original.length - from will be less than 0 .
So technically speaking, if you use such instructions:
int n = 4; Arrays.copyOfRange(bytes, bytes.length, bytes.length + n);
assumes you just want to create a new empty array of size n . In other words, you create a new array and do not copy anything from the original array.