Is it OK to pass an array as an argument in Java, like int[5] &result
?
I want to pass the reference to the array because I want to change the array in the calling function. What would the syntax be?
In Java, an array is passed by value, but the value is a reference to an array. Suppose you have an array arr
. When you pass it, you can change the array that arr
refers to, but you cannot change which array arr
refers to; i.e. inside a method, you can modify the referenced object but you cannot modify the passed variable that will still be a reference to the same object. I will try to illustrate with an example:
public static void swap(StringBuffer a, StringBuffer b)
{
StringBuffer t = a;
a = b;
b = t;
}
public static void change(StringBuffer a, StringBuffer b)
{
a = a.append("1");
b = b.append("2");
}
public static void main(String[] args)
{
StringBuffer a = new StringBuffer("First");
StringBuffer b = new StringBuffer("Second");
swap(a, b);
System.out.println(a + " " + b);
change(a, b);
System.out.println(a + " " + b);
}
Output:
First Second
First1 Second2
You can see from the example that First Second
is obtained in the output instead of Second First
. This is because in the function swap
, copies have been used which have no impact on the references in the main. Thus illustrating the fact that you cannot modify the passed variable that will still be a reference to the same object. However, the function change
brings about the desired effect because you can modify the referenced object.
For more reference see this:
Is Java “pass-by-reference”?
private void demo() {
int[] array = new int[5];
System.out.println(Arrays.toString(array)); // 0, 0, 0, 0, 0
fillArray(array);
System.out.println(Arrays.toString(array)); // 0, 1, 2, 3, 4
}
private void fillArray(int[] array) {
for (int i = 0; i < array.length; i++) {
array[i] = i;
}
}
Arrays are objects in Java. And references to objects are passed by value.
Java is pass by value. This reference will be passed by value, that is copied. It will still point at the original array.
int[] arrayToPass
This might be an unpopular way of thinking about it, but being from a C/C++ side myself, it made it easier for me.
In Java, objects are reference types. Meaning that the actual object is on the heap somewhere, and what you actually use is just a reference to that object. Thus it is simpler to think of it in terms of C pointers (as an analogy).
Classname object;
would behave similar to the below in C/C++
Classname *object;
Similarly, in the case of functions, they behave as if the parameters of the function are passed as pointers. Similar to how you would simulate pass-by-reference in C:
void Function(Classname object)
It is more like the below C version
void Function(Classname *object)
And as long as you don’t change the value of the pointer itself (in java by reallocating the variable with a new instance) you can use it to access and modify the original instance.
And to wrap it all up, all types other than the primitive types (int
, double
,boolean
, etc.) are reference types (even types like Integer
, Double
, Boolean
, and arrays).
So as others have explained already, you don’t need to add the &
for it to behave as you would expect (except certain caveats like reassignment with a new instance).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With