0

In java passing an array mimics a concept called "pass-by-reference", meaning that when an array is passed as an argument, its memory address location (its "reference") is used. In this way, the contents of an array can be changed inside of a method, since we are dealing directly with the actual array and not with a copy of the array.

Now in this example

    class ChangeIt
     {
    static void doIt(int[] arr)
    {
    arr[0]=arr[arr.length-1];
    /*arr=null;*/
    }
    }
    public class TestIt {

     public static void main(String[] args) {
    int[] arr={1,2,3,4,5};
    ChangeIt.doIt(arr);
    for(int i :arr)
        System.out.print(i +" ");

     }

    }

If I run the program i get the output 5 2 3 4 5 but if I comment this line in my program arr[0]=arr[arr.length-1]; and uncomment this line in my program arr=null; still I get the output as 1 2 3 4 5

My question is why is the java.lang.NullPointerException is not raised when passing an array mimics a concept called "pass-by-reference" ?

  • 1
    It would really help if you used different variable names for the arr inside the function, and outside of the function. Maybe change `doIt(int[] arr)` to `doIt(int[] input)` just so its easier to distinguish between the two variables. – shockawave123 Aug 13 '17 at 06:45
  • Please provide a [Minimal, Complete, and Verifiable example (MCVE)](http://stackoverflow.com/help/mcve) The code you show does not have the behavior you describe. – Timothy Truckle Aug 13 '17 at 06:51
  • doIt(int[] arr) to doIt(int[] input) That did not solve the problem – Raj Gopal Bhallamudi Aug 13 '17 at 06:55

5 Answers5

0

As you say, the "memory address" of the array is passed into the method, and as a result, you will see changes you make to the contents of that array from other places. This is the same as with all other mutable objects (you can observe this behaviour with a LinkedList as well, for example, or a StringBuilder).

But this only works if you make changes to that array itself.

If you assign your local variable that holds this "memory access" to a new array (or to null), this will not change anything in the original array.

This is not pass-by-reference after all. It is passing a "memory address" by value (and changing that value to something else in your function will not be propagated back to the caller).

Thilo
  • 250,062
  • 96
  • 490
  • 643
0

The moment you assign arr, you are changing it's reference. So it will no longer be referencing the same array you passed in the function. So now the arr outside the function is pointing to something different than the arr inside the function.

shockawave123
  • 678
  • 4
  • 15
0

You pass a copy of the "pointer" to the array to doIt. This copy arr is set to null. The original value of arr in main is not affected. You don't pass the "pointer" by value.

milbrandt
  • 1,366
  • 2
  • 14
  • 19
0

Technically, this demonstrates "passing by values (of references)", not really "passing by references".

Arrays are reference types in Java. This means that array variables store references to the actual array.

When you assign an array variable like this:

arr = null;
// or
arr = new int[5];

You are changing the reference held in the variable arr, so that it points to something else.

Note that arr in the main method is a different variable from arr in the doIt method. After you pass arr to doIt, the two different arrs both reference the same array. i.e. the reference value is copied. Then, you change the arr in doIt to point to something else! Did you just change the arr in main? No, they are two different variables. arr in main still refers to the original array.

Edit:

When you do something other than reassigning arr, like changing one of the elements of arr:

arr[0]=arr[arr.length-1];

You are directly changing the array object that the two arrs are both referring to, hence the output.

Sweeper
  • 176,635
  • 17
  • 154
  • 256
0

Array[] trick is a sort of pass-by-reference but actually is a pass-by-copied_reference. You are nullifying a copied memory address reference and the original address reference is alive in a calling context.

Whome
  • 9,979
  • 6
  • 48
  • 63
  • You are right @Whome. Ok I found the answer . When we pass an array to other method, Any changes in the content of array through that reference will affect the original array.. But changing the reference to point to a new array or assigning it to null will not change the existing reference in original method.. – Raj Gopal Bhallamudi Aug 13 '17 at 07:38