0

I wonder why the output is still 1,2,3,4,5, since if I change a value in doIt (for example : z[0] = 10), the array is changed.

public class Main 
{
    public static void main(String[] args) {
        int[] array = {0,1,2,3,4,5};
        testIt.doIt(array);

        for(int i=0;i<array.length;i++){
            System.out.println(array[i]);
        }
    }
}

class testIt
{
    public static void doIt(int [] z){
        z = null    
    }
}
1

2 Answers 2

1

Here

doIt is passed a copy of the array reference and can't change the original reference and make no difference to original array.

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

Comments

1

doIt is passed a copy of the array reference, so it can't change the original reference (stored in the array variable). It can only change the state of the object referred by this reference. Therefore setting z to null inside doIt makes no difference outside the method.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.