0

I am reviewing a practice problem and I just want to know the sequence as to how the program came up with an answer of ---> 2 1 I am mostly having trouble understanding the main driver call. I understand the usage of the methods.

The code is:

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

    int[] y = {1, 2, 3, 4, 5};
    increase(y[0]);
    System.out.println(x[0] + " " + y[0]);
  }
  public static void increase(int[] x) {
    for (int i = 0; i < x.length; i++)
      x[i]++;
  }
  public static void increase(int y) {
     y++;
  }
}
user2045470
  • 59
  • 1
  • 5
  • 11

3 Answers3

4

The code is demonstrating the difference between (effectively) call-by-reference (in the first increase method) and call-by-value (in the second increase method). In fact, both methods use call-by-value, but in the first case the value is a reference to an object (the array) and in the second case is an int (a single value from the array).

The code int[] x = {1, 2, 3, 4, 5} creates an array. When you call increase(x) you are calling the first increase method. That method iterates through the elements of the array and increments each one. The line x[i]++ is equivalent to x[i] = x[i] + 1. The results are stored back to the array. By the end of this call, the array now contains {2, 3, 4, 5, 6}. Hence x[0] is 2.

In the second call to increase(int y) we do not pass in the array, but the value of y[0] (i.e. 1). The method increments the variable y but that has no effect outside of the method. In Java, when you pass a variable it is passed by value which essentially means a copy of the value is passed in. Any changes that are made to that value do not affect the original.

When you pass in the array, you pass a reference to the array object. The reference cannot be changed, but the contents of the object (the contents of the array) can be changed.

I have to admit it is a bit confusing, but re-read what I've written a few times and hopefully you'll get it!

ᴇʟᴇvᴀтᴇ
  • 12,285
  • 4
  • 43
  • 66
  • Thank you so much!! I managed to understand the first call of increase(x), but the second call to increase(int y) was really confusing me. I understand it now and will definitely re-read over and over again to completely understand it. Thanks again! – user2045470 May 09 '13 at 18:21
1

A Java method receives a copy of the value you pass in, in case of a primitive, or a copy of the reference, in case of an object type. increase(int[]) takes in an array (which is a reference type), and increments each value in that array. The method receives a copy of your array reference, and does not change it. It just increments each element in the array, which is the same array as you sent in.

With increase(int) it's a bit different. The y variable is a copy of the value you pass in. When you increment y, you're just incrementing that copy. The original value is not changed. So the appearance is that the value you pass in is in fact not changed.

Hence, the output will be 2 1.

NilsH
  • 13,705
  • 4
  • 41
  • 59
0

In increase(x), you are passing the reference of the array and thus the increment is in fact increasing the actual values. While in the second instance you are passing a specific value of the array and increasing it is not really affecting the value held in the array. For more information, Check out the answer here

Community
  • 1
  • 1
Adarsh
  • 3,613
  • 2
  • 21
  • 37