简体   繁体   中英

Modifying local vars changes outside vars?

I'm trying to understand how the below implementation of QuickSort works in Java. I've got most of it, but I'm pretty confused how it does anything at all. When you pass in a variable to a function and modify it, it normally doesn't modify the original variable that was passed in. So why does this implementation of quicksort with no return type modify the array passed in?

public static void quickSort(int[] arr, int low, int high) {
    if (arr == null || arr.length == 0)
        return;

    if (low >= high)
        return;

    int middle = low + (high - low) / 2;
    int pivot = arr[middle];

    // make left < pivot and right > pivot
    int i = low, j = high;
    while (i <= j) {
        while (arr[i] <pivot) {
            i++;
        }

        while (arr[j] > pivot) {
            j--;
        }

        if (i <= j) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
            i++;
            j--;
        }
    }

    if (low < j)
        quickSort(arr, low, j);

    if (high > i)
        quickSort(arr, i, high);
}

because arrays are treated as Object´s, and in this case they pass the value of the reference to the method, which causes this implementation to work on the array that was passed to this method.

It´s the same as if you would do

public void test(ObjectA obj) {
    obj.setVal(1);
}

In this case you would work on the passed ObjectA and would also invoke the method setVal on this instance. In this case the method invoking inside the method test would also reflect in changes inside the passed instance of this specific objects (because it´s the same instance).

The same happens for arrays

public static void main(String args[]) {
   int[] arr = {1,2,3};
   test(arr);
   System.out.println(arr[0]); // This would print 13 now.
}

public static void test(int[] arr) {
   arr[0] = 13;
}

For further reference you could go through this question

Your int[] arr is a reference to an array. This reference is a copy of the reference passed to it, and that reference cannot be changed. However, the array it references, is not copied and when you modify it, these changes are visible to the caller.

Everything in Java is passed by value. If we are talking about non-primitives - Object variables as well as arrays, the VALUE of the variable is a LINK to the object. We can't re-assign the link, but we can change internals of the object.

The thing is that when we pass primitives - they are kept in Stack of the function and their change won't affect value of the variable in method call. But objects are in Heap and they are shared. So we can change it's internals from any place of the call stack.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM