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" ?