1
public class ABC {

    public static void main(String args[]) {
        int i[] = {1};
        change_i(i);
        System.out.println(i[0]);   //prints 1
    }
    public static void change_i(int a[]) {
        int j[]={3};
        a=j;
    }
}

public class ABC {

    public static void main(String args[]) {
        int i[] = {1};
        change_i(i);
        System.out.println(i[0]);   //prints 3
    }
    public static void change_i(int a[]) {
        int j[]={3};
        a[0]=j[0];
    }
}

What's the difference actually in these two assignments a=j; and a[0]=j[0]; Why does the output differ?

nem035
  • 31,501
  • 5
  • 73
  • 88
Praveen Kumar
  • 625
  • 2
  • 8
  • 21

1 Answers1

1

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.

Community
  • 1
  • 1
nem035
  • 31,501
  • 5
  • 73
  • 88
  • So does it mean that in a=j if the value of array j changes value of array a also changes?? – Praveen Kumar Jan 20 '15 at 16:44
  • @PraveenKumar look at my edit for more detail. As far as this specific question, `a` and `j` are just two different variables that reference the **same array** so the answer would be yes. However, you need to understand that there are no array `a` and array `j`, the are both one array with `a` and `j` being referential variables to the array. – nem035 Jan 20 '15 at 16:56