basically, the output for the code below is 2. Could someone explain to me why that is so? Thank you so much :).
int[][] x = {{3,1,4},{1,5,9}};
int[] y = {2,6,7};
y = x[1];
y[1] = 1;
System.out.println (x[0][1] + x[1][1]);
The problem you are having is that both y and x[1] reference the same array. See the comments below for more information.
int[][] x = {{3,1,4},{1,5,9}}; // x[0] references an array {3,1,4}; x[1] references an array {1,5,9}
int[] y = {2,6,7}; // You create the array {2,6,7} assigning its reference to y
y = x[1]; // While y was referencing the array {2,6,7} you overwrite this reference by the reference stored at x[1]. This reference is pointing at the array {1,5,9}. So now both y and x[1] are pointing to (or referring to in Java slang) the same array!
y[1] = 1; // So now you are changing the array {1,5,9} to {1,1,9} (y[1] is the same as x[1][1]
System.out.println (x[0][1] + x[1][1]); // x[0][1] (meaning the second value from the first array (referenced by x), which value is 1.) x[1][1] (meaning the second value from the second array, which value is also 1.) So 1 + 1 is in fact 2.
Before I start explaining, keep this in mind: An int is a primitive type; an array is an object.
Take a look at this simple code:
int[] x = {1,2,3};
int y = x[0];
y = 5;
System.out.print(x[0]);
the output is 1 and not 5 for the following reason:
y is being assigned the value of x[0], essentially getting a copy of it. It does not point to x[0] so any future changes to y do not apply to x.
In your situation with two-dimensional arrays, you have the following code:
int[][] x = {{3,1,4},{1,5,9}};
int[] y = {2,6,7};
y = x[1];
y[1] = 1;
System.out.println (x[0][1] + x[1][1]);
The essential difference here is the following:
y is being assigned the value of the reference of x[1], which here too is a copy, but since it is a reference (because x[1] is an array) it does not really matter, cause the original and the copy both point to the same object. Whether you use y or x[1] you are referring to the same object, even though these are two different references.
Because variables containing arrays in java (like other objects) are just references to these objects
In more details:
At start:
This is easy.
Then
When we do y = x[1], we don't change the values of y. Instead, what happens is that y now points to x[1]
In memory, we have somewhere the array {1, 5, 9} and two variables are looking there (x[1] and y). This also means that in memory, we still have somewhere the array {2, 6, 7} but no variable are looking at (pointing at) it (since y changed to look at the same location as x[1]). In java, the garbage collector automatically finds what memory location aren't used anymore and cleans the memory
So finally
Changing either x[0] or y will access the same place in memory so the value will change for both variables. doing
y[1] = 1;
is exactly the same as doing
x[1][1] = 1;
When doing so, we change the array and it becomes {1, 1, 9}
So x[0][1] + x[1][1] is equal to 1+1
This will occure all the time when you program, since this is the same behaviour for all Objects.
Side note You will see that many people argue on the exact terminology when speaking of references in Java. Java does not pass by reference in the same way as in c++. What you shoud remember for a start is how variables behave when assigned to arrays or objects
Our starting position is
int[][] x = {{3,1,4},{1,5,9}};
int[] y = {2,6,7};
which can be shown as
x → { [0] , [1] }
↓ ↓ // each 1D array like {3,1,4} can be also shown as
{3,1,4} {1,5,9} // { [0] [1] [2] }
// ↓ ↓ ↓
y → {2,6,7} // 3 1 4 but I skipped that for simplicity
(since any 2D array in Java is simply 1D array of other 1D arrays).
When we execute y = x[1]; y will now be reference to same array which is stored in x[1] (it is not copy of original array with same elements, it is same array).
So situation bow is similar to
x → { [0] , [1] }
↓ ↓
{3,1,4} {1,5,9}
↑
y ──────────────────────┘
This means that we can change that same array via two references y and x[1] and each change can be also seen via other reference.
So if we do
y[1] = 1;
it will also affect array held by x[1] like
x → { [0] , [1] }
↓ ↓
{3,1,4} {1,1,9}
↑ └─changed
y ─────────────────────┘
Now x looks like {{3,1,4},{1,1,9}};.
x → {
├[0] → {
│ ├─[0] → 3
│ ├─[1] → 1 //x[0][1]
│ └─[2] → 4
│ }
└[1] → {
├─[0] → 1
├─[1] → 1 //x[1][1]
└─[2] → 9
}
}
So since x[0][1] = 1 and x[1][1]=1 their sum is 2.
this why :
int[][] x = {{3,1,4},{1,5,9}};
int[] y = {2,6,7};
y = x[1]; //{1, 5, 9}
y[1] = 1; //{1, 1, 9}
System.out.println (x[0][1]/* {3,*1*,4} */ + x[1][1] /* {1, *1*, 9} */);
in Java when you assign array like these y = x[1]; or pass array any way possible (to method for example) you actually pass copy of reference x[1] array to another one, array y have same reference as x to GC where array exsis and when you change the value y[1]
y[1] = 1; //{1, 1, 9}
you actually change value are share it between y[1] and x[1][1]
1 + 1 = 2This is some basic Java...int[] yto beint[][] xsecond row. So whatever you do toywill also occur tox. You want to useArray.clone()orArray.copy()so you don't run into this issue.