Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass array by reference in Java

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?

like image 403
maryam Avatar asked Dec 27 '12 22:12

maryam


4 Answers

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”?

like image 24
bane Avatar answered Oct 20 '22 04:10

bane


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.

like image 127
JB Nizet Avatar answered Oct 20 '22 04:10

JB Nizet


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
like image 5
RNJ Avatar answered Oct 20 '22 04:10

RNJ


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).

like image 2
Karthik T Avatar answered Oct 20 '22 04:10

Karthik T