1

I am looking through some old questions for a test and I come across a quite easy question however it wasn't what I expected.

public class Exampractice {
  public static void f(int x, int[] y, int[] z) {
    x = 2;
    y[0] = x;
    z = new int[5];
    z[0] = 555;
  }
  public static void main(String[] args) {
    int x = 111;
    int[] y = {222, 333, 444, 555};
    int[] z = {666, 777, 888, 999};
    f(x, y, z);
    System.out.println(x);
    System.out.println(y[0]);
    System.out.println(z[0]);
  }
}

The question asks what is the result of the following code. I get the following results:

111 
2 
666 

I understand why x is 111 as local variables override all other variables, y[0] =2 as the code says it equals x which is 2 but I am lost on why z[0] = 666 as it has been rearranged in the f class.

Subir Kumar Sao
  • 8,171
  • 3
  • 26
  • 47
Softey
  • 1,451
  • 3
  • 21
  • 42
  • possible duplicate of [Is Java "pass-by-reference"?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference) – Brian Roach Apr 03 '14 at 10:51
  • It is fairly easy to understand if you remember few things: 1) in Java all variables are passed by value - i.e. you get a copy of the primitive data types and a copy of the reference to a complex data type. 2) `new` operator creates completely new object, it does **not** update the existing object. – Germann Arlington Apr 03 '14 at 10:55

5 Answers5

6

In Java, the object reference is passed as a value. Therefore, when z = new int[5]; is executed, the local array variable z present in the f() method now refers to the newly created int array and nothing happens to the original array whose reference was passed as a value to it when the method was called.

public static void f(int x, int[] y, int[] z) {
    x = 2;
    y[0] = x;
    // Till here z is referring to the int array passed from main method
    z = new int[5]; // now z is re-assigned with a new reference, the one of the newly created int array
    // thus the reference to the original array is no more being used here
    z[0] = 555; // this modifies the values of the newly created array 
}

Personally, I always suggest reading this answer by Eng.Fouad to understand this concept.

Community
  • 1
  • 1
Rahul
  • 44,383
  • 11
  • 84
  • 103
1

Because you are using new which creates new object

ruhungry
  • 4,506
  • 20
  • 54
  • 98
1

What matters is whether the value or reference is set to variable

x has value

    x=111

and

    y and z
    points two different arrays

y and z are not having value it has a reference of an array

    reference y[0] is edited globally  in f

    z is recreated in the method f locally using "new" 

It does not affect original object in method main() so we get that result.

saravanakumar
  • 1,747
  • 4
  • 20
  • 38
0

The method creates a new int array. The old one is intact after the method exits.

Niels Bech Nielsen
  • 4,777
  • 1
  • 21
  • 44
0

Z is a new object, the z in the function is actually this.z

Ofer
  • 4,879
  • 6
  • 20
  • 26