You clone() or System.arraycopy() on get() if you want to ensure that the entire graphic object (containing the array) is immutable. This is usually done when the API that provides the array is publicly available and there are restrictions on the values in the array or when multiple threads access objects. In such cases, immutability is important.
Say you have a GroceryStore object that has a getItemsSortedByPrice() method. You store elements in an array that maintain order by price, but if you return this array, client code can change it and break the (internal) invariants of your object.
If this is internal code (i.e.) that is not part of the public API, and you know that you will not modify the array, then cloning / copying is probably not necessary, as this is detrimental to performance without real benefit.
It all depends on the context.
Arrays are just objects, and all (im) rules / variation methods applicable to ordinary objects also apply to arrays.
Op De Cirkel
source share