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.

2
  • possible duplicate of Is Java "pass-by-reference"? Commented Apr 3, 2014 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. Commented Apr 3, 2014 at 10:55

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I see how it works now... Took a little while to get my head around it
1

Because you are using new which creates new object

Comments

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.

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.