Your use of the "n" parameter, as noted above, is redundant, but also corrupted by code with n + 1 ?? Your code will throw an ArrayIndexoutOfBoundsException if you use something like:
String [][] m1 = { {"A", "B"}, {"C", "D" } }; String [][] m2 = copy(m1, 2);
String [][] m1 = { {"A", "B"}, {"C", "D" } }; String [][] m2 = copy(m1, 2);
What, apparently, is how you are going to call it?
It also limits your function to square "matrixes" of strings. But as for the problem you quoted, I see no reason why the program should behave this way ... I even ran it using the above call (but with n = 1 ???), and then changed
m2[0][1] = "X";
and m1 did not affect as expected. Even replacing the innermost line of code with:
out[i][j] = matrix[i][j];
does not change this, since the compiler rewrites it to what you originally had. Actually a lot of String syntax is just the syntactic sugar for StringBuffers (like concatenation and assignment). for example, the compiler will rewrite
String s = "Hello "; s += "World"; // Makes it appear that String is builtin type!
for
String s = new String("Hello "); s = new StringBuffer(s).append("World").toString();
This is why you have a lot of string concatenation inside loops that they can do very poorly.
I do not understand why you have a problem that you have indicated.
And since you are not changing the matrix parameter, Pass By Reference has nothing to do with it.