6

Analyze the following code:

class Test {

    public static void main(String[] args) {
        int[] x = {1, 2, 3, 4};
        int[] y = x;

        x = new int[2];
        for (int i = 0; i < y.length; i++) {
            System.out.print(y[i] + " ");
        }
    }
}
  • A. The program displays 1 2 3 4
  • B. The program displays 0 0
  • C. The program displays 0 0 3 4
  • D. The program displays 0 0 0 0

The answer for the following code is A. But why the answer is not B ?

4
  • 1
    x and y are references. Commented Dec 25, 2015 at 11:31
  • because you are printing the values of: {1,2,3,4}. How could the answer possibly be anything but A? Commented Dec 25, 2015 at 11:31
  • because after assigning initial value of x to y then only you have changed the value of x which means it won`t affect the current value of y now. So when you print out y then it prints the value which corresponds to the previous value of x Commented Dec 25, 2015 at 11:41
  • 1
    I think it would be better to post text instead of image. Commented Dec 25, 2015 at 11:44

5 Answers 5

9

Let's assume that {1, 2, 3, 4} has memory address M.

When assigning x to {1, 2, 3, 4}, you're assigning a reference to the memory address of {1, 2, 3, 4}, i.e. x will point to M.

When assigning y = x, then y will refer M.

After that, you're changing the reference where x points to, let's say it's N.

So, when printing, y points to M (which is the address of {1, 2, 3, 4}), but x holds reference to N (which is new int[2]). And here comes the difference.

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

Comments

4

To give you a more clear explanation:

int[] x = {1, 2, 3, 4};       // step 1

enter image description here


int[] y = x;    // step 2

enter image description here


 x = new int[2];        // step 3

enter image description here


In the third step, when the x changes, y is not affected because you are changing the reference of x, not the address of the array. So it doesn't affect the value of y.

Comments

1

Because int[] y = x; the array y will have some variables like this: {1,2,3,4}.

This line x = new int[2]; will creating a different space for x pointing to it.

So the result will be {1,2,3,4} of the array y

Comments

1

Because You are assigning the value of x to y not giving reference of x to y.

Comments

1

The answer is definitely A. I will explain it a little bit more step by step:

int[] x = {1, 2, 3, 4};
int[] y = x;

From the above lines, we now know that x and y are equal to each other in values and their length is 4.

x = new int[2];

We have now changed the value of x, but we haven't changed the value of y.

So now x has a length of 2 which is {0, 0}, and y still has a length of 4 which is {1, 2, 3, 4}, and here comes the difference...

for (int i = 0; i < y.length; i++)

In the above line, we already know that y.length = 4, so it's i < 4.

System.out.print(y[i] + " ");

The above line will now print the array of values of y respectively which are:

1

2

3

4

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.