0

I am given these 2 methods and when i run question8 it prints out "1 3" but I don't understand why. Shouldn't the doSomething method not affect the "arr" array in the question8 method and therefore print out "2 4"

private static void question8() {
    int[] arr = {1,2,3,4};
    doSomething(arr);
    System.out.print(arr[1] + " ");
    System.out.print(arr[3]);
}
private static void doSomething(int[] list) {
    int[] b = list;
    for (int i = 0; i < b.length; i++) {
        b[i] = i;
    }
}
herteladrian
  • 381
  • 1
  • 6
  • 18

1 Answers1

7

Array is an Object, The reference variable is passed by value. Same object is accessed. In your code,

private static void doSomething(int[] list) {
int[] b = list; // this means list and b are referring to the same array that was being referenced by list reference variable
for (int i = 0; i < b.length; i++) {
    b[i] = i;
  }
}

This is how it goes

  • You create the array in question8() method.
  • You are passing it to doSomething() method from question8()
  • The same array object is passed to doSomething() method which is being referred by arr variable in question8() method.
  • Now again, you are doing int[] b = list which means variable b is referring to the array object that is being referred by variable list. Again the array object is same.

To understand this thing properly you can read about pass by value in java with primitives and reference variables. The reference variable part is useful for you.

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
  • I thought all parameters were passed by value in java so the method just receives a separate copy of whatever data was passed to it. So if that is true, why does the the method doSomething alter the original arr array even though it only uses a copy of it? @Prasad Kharkar – herteladrian Jan 13 '14 at 08:31
  • 3
    @user1702529: parameters are indeed passed by value. But what is passed is not an object or an array, but a *reference to an object* or a *reference to an array*. If you assign another array to the list argument, the variable in the main method won't be affected. – JB Nizet Jan 13 '14 at 08:33
  • @user1702529. What you know is true. Whatever data is passed to the method is copied. Only difference is that whole array is not being passed. Only the reference variable that refers to array objects is copied into another method. Hence referring the same array object. – Prasad Kharkar Jan 13 '14 at 08:34
  • OHH okay. So when I pass an object as a parameter, it passes a copy of the reference of the original object i passed, thus any changes i make in the method are also changing the original object. Yet if i pass a primitive as a parameter such as int or double then the method receives a copy of the actual content, not a reference to the original primitive, thus any changes made to the primitive in the supplementary method wont affect the original primitive Is this correct? @PrasadKharkar, JBNizet – herteladrian Jan 13 '14 at 08:40
  • @user1702529, yes sir. – Prasad Kharkar Jan 13 '14 at 08:41
  • and lastly can you give me an example with code of the doSomething method in which, as you said " the variable in the main method won't be affected" would it look like this int b = new int[list.length] for (int i = 0; i < b.length; i++) { b[i] = list[i] } @JBNizet – herteladrian Jan 13 '14 at 08:41
  • or @PrasadKharkar could you confirm that last bit of code i typed for me? – herteladrian Jan 13 '14 at 08:45