In line 8, a copy of arr is made with one extra slot. The new array is assigned to arr, replacing the original array.
Method 2: Using ArrayUtils.add() (Apache Commons Lang)
If you have Apache Commons Lang on your classpath, you can use the ArrayUtils.add() method to append an element to an array in a more readable and simplified way. This method performs the same operations under the hood as Arrays.copyOf(), but it abstracts the details, making your code cleaner.
int[] arr = { 10, 20, 30 };
arr = ArrayUtils.add(arr, 40);
Under the hood, add performs the same three steps described in the beginning. It just makes the process more readable.