Taking into consideration that Java operates off of pass-by-value. Which of the following would be deemed the most conventional/correct way to assign a new array value to an already existing array:
1:
int[] x = {1,2};
public static int[] changeValue(int[] arr) {
for (int i = 0; i < arr.length; i++) {
arr[i]*=2;
}
return arr;
}
changeValue(x);
2:
int[] x = {1,2};
public static int[] changeValue(int[] arr) {
for (int i = 0; i < arr.length; i++) {
arr[i]*=2;
}
return arr;
}
x = changeValue(x);
3:
int[] x = {1,2};
public static void changeValue(int[] arr) {
for (int i = 0; i < arr.length; i++) {
arr[i]*=2;
}
}
changeValue(x);
Thanks.
Streamoption?voidreturn type explicitly says that the input array will be modified. Returningint[]might be misleading. A user might think a new array is returned, while it is not. But still, prefer immutability.