for example,
private char[][]board ;
public voi foo(char[][] board) {
this.board = board; //so, what's happening in this line. just pass the reference?
//
}
Also, if I modify this.board, will it reflect to original board?
for example,
private char[][]board ;
public voi foo(char[][] board) {
this.board = board; //so, what's happening in this line. just pass the reference?
//
}
Also, if I modify this.board, will it reflect to original board?
Yes, it's just copying the reference, not the data.
To make it more obvious, let's change your variable names:
private char[][] firstBoard;
public void foo(char[][] secondBoard) {
this.firstBoard = secondBoard;
}
After you call the foo()
function, both firstBoard
and secondBoard
will point to the same 2D array. So if you then do this:
secondBoard[0][0] = 'X';
System.out.println(firstBoard[0][0]);
...then 'X' will be printed out, since changing the array that secondBoard points to is changing the same array that firstBoard points to. I'm surprised that you didn't write a little test program to test this out.
Recommended reading:
Cup Size -- a story about variables and Pass-by-Value Please
In your case the change on any array will be applied to another array, so If you want to keep using both arrays you should copy data one by one like this:
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
t.board[i][j]=board[i][j];
}
}
here after copying, you can change array with changing the other one.