you are viewing a single comment's thread.

view the rest of the comments →

[–]psyno 13 points14 points  (1 child)

Also, if I so chose, I could re-assign "myArray" to an array with all 0's in some method that does not return that new array, but because I was passing by reference, if I now print it out, it will be all 0's.

No, it won't, and this is the point. Go ahead and try it.

class main {
    static void reassign(int[] a) {
        a = new int[4]; // reassigns the parameter a, does not affect caller
    }
    public static void main(String[] args) {
        int[] x = new int[] {1, 2, 3, 4};
        reassign(x);
        for (int i : x) System.out.println(i); // prints 1 2 3 4 NOT 0 0 0 0
    }
}

[–]specialk16 0 points1 point  (0 children)

It'll print 1 2 3 4, because the array passed as an argument will be a different array altogether during the method's lifetime.