1) a=j
Makes the variable a
reference whatever variable j
is referencing. Meaning, after the statement is executed, a
and j
reference the same thing.
2) a[0]=j[0]
Makes the value of the first element (at index 0) of array a
have the value of the first element of array j
. Meaning, after the statement is executed, the first element of a
and j
reference the same thing
EDIT
As far as specifically to your question, within the method change_i
, the following happens for each statement:
1)
public static void change_i(int a[]) { // pass the array a by value
int j[]={3}; // create a new array of integers with size 1 that holds the number 3 as its only element
a=j; // make this passed copy value of array a now point to j
}
Java passes everything by value (Look here for detail) so after the method change_i
is executed, nothing changes. The reason is that variable a
inside change_i
method serves as a reference to the array a
that was passed into the method. After you execute the statement a=j
, this variable a
now references the array j
that was created inside the method however nothing affects the array that passed in. Only thing that changes is what a
, within changes_i
, references
2)
public static void change_i(int a[]) { // pass the array a by value
int j[]={3}; // create a new array of integers with size 1 that holds the number 3 as its only element
a[0]=j[0]; // used the copy value of array a to access the first element of original array a and give it the value of the first element of array j, which is 3
}
This method actually alters the array that was passed in. Since the variable a
inside this method references the array that was passed in, when we call a[0]
, we are using this copied reference to access the first element of the original array and thus a[0]=j[0]
actually alters the array.
You should read more about how Java passes values into methods.