How do I clone a java byte array? - java

I have a byte array which i want to copy/clone to avoid calling code from modifying my internal representation.
How do I clone a java byte array?

JLS 6.4.5 The Members of an Array Type
The members of an array type are all of the following:
The public final field length, which contains the number of components of the array (length may be positive or zero).
The public method clone, which overrides the method of the same name in class Object and throws no checked exceptions. The return type of the clone method of an array type T[] is T[].
All the members inherited from class Object; the only method of Object that is not inherited is its clone method.
Thus:
byte[] original = ...;
byte[] copy = original.clone();
Note that for array of reference types, clone() is essentially a shallow copy.
Also, Java doesn't have multidimensional arrays; it has array of arrays. Thus, a byte[][] is an Object[], and is also subject to shallow copy.
See also
Wikipedia/Object copy
Java Nuts and Bolts/Arrays
Related questions
Deep cloning multidimensional arrays in Java… ?
How to effectively copy an array in java ?
How to deep copy an irregular 2D array
How do I do a deep copy of a 2d array in Java?
Other options
Note that clone() returns a new array object. If you simply want to copy the values from one array to an already existing array, you can use e.g. System.arraycopy (jdk 1.0+).
There's also java.util.Arrays.copyOf (jdk 1.6+) that allows you to create a copy with a different length (either truncating or padding).
Related questions
Difference between various Array copy methods

System.arraycopy(src, 0, dst, 0, src.length);

It's easy, and it's a great idea to do it.
byte[] copy = arr.clone();
Note that the return type of the clone() method of arrays is the type of the array, so no cast is required.

In order to avoid a possible Null Pointer Exception I use the following syntax:
byte[] copy = (arr == null) ? null : arr.clone();

Related

What does new ClassName[0] do? (Java 8)

What does this syntax do, with square brackets around the number?
new Integer[0];
I've found it in a codebase I'm maintaining but I can't find any documentation on it. It is used like this:
Set<Form> forms = getForms();
List<Form> formsList = Arrays.asList(forms.toArray(new Form[0]))
It allocates an array with length zero; e.g. new Integer[0] creates a zero length array of Integer objects.
Why would you do that?
Well look at the javadocs for the form.toArray(T[]) method. Assuming that form is some subtype of Collection they are here.
The purpose of the toArray method is to copy the elements of the target collection (e.g. your form) into an array:
If the argument array is large enough to hold all elements, they are copied into that array. The result will be the argument array.
If the argument array is too small, a new array is allocated with the same type as the argument array and a length that is (just) enough to hold the elements. The elements are then copied into the new array, and it is returned as the result.
So what the code is actually doing is copying the elements of form to an Integer[] of the right size, and then wrapping the array to give a (fixed sized) List<Integer>. This can then be passed to some other code without worrying that that code might alter the original form collection.
Unlike traditional array which store values like string, integer, Boolean, etc. array of objects stores objects. The array elements store the location of reference variables of the object
Syntax:
Class obj[]= new Class[array_length]

What exactly is wrong with newarray = oldarray.clone() for md arrays?

After some search for copying of multidimensional arrays manipulation in java I found this question: copy a 2d array in java and a downvoted answer suggests using the method in the title. So my questions are:
1) why is it bad? (becuase it looks quick and obvious)
2) is the best way still System.arraycopy each line?
Fundamentally, an array is an object. If you have a multidimensional array and you clone it, you won't get copies of the internal arrays (because they are also array Object(s)). You could use Arrays.copyOf(T[],int). From the linked Javadoc,
Copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length. For all indices that are valid in both the original array and the copy, the two arrays will contain identical values. For any indices that are valid in the copy but not the original, the copy will contain null. Such indices will exist if and only if the specified length is greater than that of the original array. The resulting array is of exactly the same class as the original array.
Edit
The above is analogous to System.arraycopy() but also creates shallow copies. If you need to create deep copies of the multidimensional array you would have to iterate over the array clone and create a deep copy for each row.

Do I have to clone() an array of floats?

If I've got float[] a = {1,2,3};, can I copy it using only float[] b = a; to obtain a deep copy? How do I know when I need to use a.clone()?
To clarify: I want to be able to change a (e.g. a[0]=4;) without affecting b.
Just making float[] b = a; is not enough because then the array object will be shared between the 2 references a and b.
But because you have an array of primitive types is enough to use float[] b = a.clone(). The clone in this case will make a deep copy and will duplicate the array object including the primitive values inside.
If you had an array of some reference type you would need to clone the array and than iterate and clone every object to get a deep copy.
Shallow copying means only making the new reference to point to the memory location of the old array, while deep copying means allocating a new memory location and copying the contents of the first array to other.
Use deep copy only when you are afraid that someone will nullify/delete the first array. In that case your second array will also be pointing to non existent memory or null. If you have a requirement to save the contents in the second array then go for deep copy otherwise use shallow copy.
Read more about shallow and deep copy here: http://en.wikipedia.org/wiki/Object_copy
This is how you can make shallow or deep copy of your array:
Shallow copy
float[] b = a;
Deep copy
float[] b = Arrays.copyOf(a);
float[] b = a; means you are assigning the reference of the Array object held by variable a to variable b.
Use Arrays.copyOf() or System.arraycopy() to clone an array in Java. Also look at the micro-benchmarking.
How do I know when I need to use a.clone()?
Whenever you write a = b in Java where a and b are not primitives (i.e. boolean, char, byte, int, long, float, double), you are copying a reference.
In your example, when writing float[] a = {1,2,3}; then float[] b = a;, you only create one array, but have two variables "pointing" to it. So any change made to the array will be reflected on a and b.
To get a deep copy of your array and achieve your goal, simply clone it:
float[] b = a.clone();
no you need to do it for example like:
float[] b = (float[])a.clone();
I don't think that you can clone primitives, but if you had to you could always create a new array and a loop to copy all the values of one into the other.

How is length implemented in Java Arrays?

I was wondering about the implementation of length of a Java Array. I know that using arrayName.length gives us the number of elements in the array, but was wondering if this is a method / function or it is just a data member of Array?
I guess it must be a data member as we do not use parenthesis() when invoking it. But if it is a data member how/when is the value of this length assigned/computed?
According to the Java Language Specification (specifically §10.7 Array Members) it is a field:
The public final field length, which contains the number of components of the array (length may be positive or zero).
Internally the value is probably stored somewhere in the object header, but that is an implementation detail and depends on the concrete JVM implementation.
The HotSpot VM (the one in the popular Oracle (formerly Sun) JRE/JDK) stores the size in the object-header:
[...] arrays have a third header field, for the array size.
You're correct, length is a data member, not a method.
From the Arrays tutorial:
The length of an array is established when the array is created. After creation, its length is fixed.
If you have an array of a known type or is a subclass of Object[] you can cast the array first.
Object array = new ????[n];
Object[] array2 = (Object[]) array;
System.out.println(array2.length);
or
Object array = new char[n];
char[] array2 = (char[]) array;
System.out.println(array2.length);
However if you have no idea what type of array it is you can use Array.getLength(Object);
System.out.println(Array.getLength(new boolean[4]);
System.out.println(Array.getLength(new int[5]);
System.out.println(Array.getLength(new String[6]);
Yes, it should be a field. And I think this value is assigned when you create your array (you have to choose the length of array while creating, for example: int[] a = new int[5];).
I believe its just a property as you access it as a property.
String[] s = new String[]{"abc","def","ghi"}
System.out.println(s.length)
returns 3
if it was a method then you would call s.length() right?
From the JLS:
The array's length is available as a
final instance variable length
And:
Once an array object is created, its
length never changes. To make an array
variable refer to an array of
different length, a reference to a
different array must be assigned to
the variable.
And arrays are implemented in the JVM. You may want to look at the VM Spec for more info.
It is a public final field for the array type. You can refer to the document below:
http://java.sun.com/docs/books/jls/third_edition/html/arrays.html#10.7
Every array in java is considered as an object. The public final length is the data member which contains the number of components of the array (length may be positive or zero)
Java arrays, like C++ arrays, have the fixed length that after initializing it, you cannot change it. But, like class template vector - vector <T> - in C++ you can use Java class ArrayList that has many more utilities than Java arrays have.

Difference between various Array copy methods

What is the difference between
System.arraycopy(),
clone()
manual copying by iterating through the elements
Arrays.copyOf()
and just doing arraynew = arrayold?
System.arraycopy() uses JNI (Java Native Interface) to copy
an array (or parts of it), so it is
blazingly fast, as you can confirm
here;
clone() creates a new array with the same characteristics as the old array, i.e., same size, same type, and same contents. Refer to here for some examples of clone in action;
manual copying is, well, manual copying. There isn't much to say about this method, except that many people have found it to be the most performant.
arraynew = arrayold doesn't copy the array; it just points arraynew to the memory address of arrayold or, in other words, you are simply assigning a reference to the old array.
System.arraycopy() copies data from one existing array into another one and depending on the arguments only copies parts of it.
clone() allocates a new array that has the same type and size than the original and ensures that it has the same content.
manual copying usually does pretty much the same thing than System.arraycopy(), but is more code and therefore a bigger source for errors
arraynew = arrayold only copies the reference to the array to a new variable and doesn't influence the array itself
There is one more useful option:
Arrays.copyOf() can be used to create a copy of another array with a different size. This means that the new array can be bigger or larger than the original array and the content of the common size will be that of the source. There's even a version that makes it possible to create an array of a different type, and a version where you can specify a range of elements to copy (Array.copyOfRange()).
Note that all of those methods make shallow copies. That means that only the references stored in the arrays are copied and the referenced objects are not duplicated.
Arrays.copyOf(..) uses System.arrayCopy(..) method internally.
There are answers but not a complete one.
The options considered are
Arrays.copyOf()
System.arraycopy()
Below is the java implementation of Arrays.copyOf()
public static double[] More ...copyOf(double[] original, int newLength) {
double[] copy = new double[newLength];
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
As you can see copyOf uses System.arraycopy internally.
If you already have an array created use System.arraycopy() to copy
If you need the result in a new array use Arrays.copyOf() to copy
Note: There is no point in comparing the speed obviously because their functionalities differ.

Categories

Resources