I don't know what is confusing you. System.arraycopy()
copies elements from one array to another. It doesn't play with reference.
Array#clone()
javadoc states
The general intent is that, for any object x, the expression:
x.clone() != x
Therefore you will get different references.
More detail
StringBuffer[] arr = {new StringBuffer("abc"),new StringBuffer("def"),
new StringBuffer("ghi")};
StringBuffer[] arr2 = arr.clone();
StringBuffer[] arr3 = new StringBuffer[3];
You have 3 different references. Then, see code comments
System.arraycopy(arr, 0, arr3, 0, 2); // copy values from arr into arr, the references haven't changed
//check for identity
System.out.println(arr==arr2); // different references
System.out.println(arr==arr3); // different references
Arrays.copyOf()
returns a new array (new reference) with values from the source array.
Example eg3 = new Example(eg1.foo,Arrays.copyOfRange(eg1.bar, 0, 2));
Therefore
System.out.println(eg3.bar==eg1.bar);
is false
.
And more
Example eg4 = new Example(eg1.foo,eg1.bar.clone());
System.out.println(eg4.bar==eg1.bar);
clone()
returns a new reference, so you are again comparing different references and get false
.
If you're looking to compare the contents of the arrays, use
Arrays.equals(arr, arr2); // for example
Note that here it doesn't matter because of primitive types, but if your array held reference types, you might care about shallow or deep copies.