3
public class TestArray {
    public static void main(String[] args) {
        int[] ar = {1,2,3,4,5,6,7,8,9};

        shiftRight(ar);
        for (int i = 0; i < ar.length; i++) {
            System.out.print(ar[i]);
        }
        // prints: 912345678 -- good
        System.out.println();

        reverseArray(ar);
        for (int i = 0; i < ar.length; i++) {
            System.out.println(ar[i]);
        }
        // prints: 91234567 -- I don't understand       
        System.out.println();       
    }
    public static void shiftRight(int[] ar) {
        int temp = ar[ar.length - 1];
        for (int i = ar.length - 1; i > 0; i--) {
            ar[i] = ar[i - 1];
        }
        ar[0] = temp;
    }
    public static void reverseArray(int[] ar) {
        int[] temp = new int[ar.length];
        for (int i = 0, j = temp.length - 1; i < ar.length; i++, j--) {
            temp[i] = ar[j];
        }
        ar = temp;
        for (int i = 0; i < ar.length; i++) {
            System.out.print(ar[i]);
        }
        // prints: 876543219
        System.out.println();
    }
}

Passing an array to a parameter results in passing the reference to the array to the parameter; if an array parameter is changed within the method, that change will be visible outside of the method.

The first method, shiftRight, does what I expect it to: it changes the array outside of the method.

The second method, however, does not change the array outside of the method. But running the for loop inside of the method prints the correct values. Why isn't the reference of ar pointed to temp? Is it because the variable temp is destroyed when the method stops--does that kill the reference as well? Even if this is the case, why does Java take ar, which was pointed to the reference of temp and then reapply it the the original reference of ar?

Thank you.

4 Answers 4

3

In Java, it's a misnomer to say that objects are passed by reference. It's more accurate to say that the reference to the object is passed by value.

You pass the array reference to reverseArray by value. The local parameter is a copy of the reference to the array. Later when you say

ar = temp;

You have only pointed the local ar to temp, not the original array reference ar from main.

On the other hand, in the shiftRight method, you have directly accessed the array through the copied reference, so the original array's contents change and the method works as expected.

Sign up to request clarification or add additional context in comments.

7 Comments

So while the passed value to the parameter is the reference to the array, the parameter itself is its only variable (which points to the same reference originally) that when pointed to a local array does not point the outside variable to that reference?
Yes. That is true of all parameter passing in Java.
Yes, ar = temp assigns your local ar reference to the array referred to by temp.
@Michal I knew that to be the case for primitive values but conflated the reference with the variable for the array.
For more information on how Java passes parameters, see Is Java “pass-by-reference”?.
|
0

reverseMethod receives its own reference to the array and cannot touch the caller's reference.

In other words, it receives the reference by value; if it chooses to store a new value in the local variable which holds it, then that's fine, but it has no effect on other places where references to the old value might be stored.

Comments

0

The problem is that you create a local variable temp array, and you set ar=temp. You need to actually modify the contents of ar, rather than creating a new local array and pointing your copied ar variable to temp

Try something like this.

public static void reverseArray(int[] ar) {
    int[] temp = new int[ar.length];
    System.arraycopy( ar, 0, temp, 0, ar.length );
    for (int i = 0, j = temp.length - 1; i < ar.length; i++, j--) {
        ar[i] = temp[j];
    }

    for (int i = 0; i < ar.length; i++) {
        System.out.print(ar[i]);
    }
    // prints: 876543219
    System.out.println();
}

Comments

0

When you assign the value of temp to ar

ar = temp;

you set the pointer of the method's argument to that value, which does not modify the reference of ar in the main method in any way.

If you want your modifications to "stick", then return the value from that method and assign it in main, like so:

public static void main(String[] args) {
    int[] ar = {1,2,3,4,5,6,7,8,9};

    ar = reverseArray(ar);
    System.out.println();       
}

public static int[] reverseArray(int[] ar) {
    int[] temp = new int[ar.length];
    for (int i = 0, j = temp.length - 1; i < ar.length; i++, j--) {
        temp[i] = ar[j];
    }
    ar = temp;
    for (int i = 0; i < ar.length; i++) {
        System.out.print(ar[i]);
    }
    // prints: 876543219
    System.out.println();

    return ar;  // might as well return temp
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.