public class ArrayCopy { public static void main(String[] args) { int a[] = { 1, 8, 3 }; // Create an array b[] of same size as a[] int b[] = new int[a.length]; // method-1 deep cloning Copying elements of a[] to b[] for (int i = 0; i < a.length; i++) b[i] = a[i]; // method-2 shallow cloning Copying elements of a[] to b[] System.arraycopy(a, 0, b, 0, 3); // method-3 shallow cloning int cloneArray[] = a.clone(); // will print false as shallow copy is created System.out.println(a == cloneArray); for (int i = 0; i < cloneArray.length; i++) { System.out.print(cloneArray[i] + " "); } // Changing b[] to verify that // b[] is different from a[] b[0]++; System.out.println(""); for (int i = 0; i < a.length; i++) System.out.print(a[i] + " "); System.out.println(""); for (int i = 0; i < b.length; i++) System.out.print(b[i] + " "); } } /* public static void arraycopy(Object source_arr, int sourcePos, Object dest_arr, int destPos, int len) Parameters : source_arr : array to be copied from sourcePos : starting position in source array from where to copy dest_arr : array to be copied in destPos : starting position in destination array, where to copy in len : total no. of components to be copied. Explanation: When System.arraycopy is used, it copies the elements of the source array to the destination array. For primitive types (e.g., int, char), the actual values are copied. However, for non-primitive types (e.g., objects), only the references are copied, not the objects themselves. This means both arrays will refer to the same objects in memory. For primitives, it behaves like a deep copy (values are independent). For objects, it is a shallow copy (references are shared). The clone() method in Java, provided by the Object class, performs a shallow copy by default. */