Question:
public class Test1 {
public static void main(String[] args) {
String s1 = "hello";
int mas[] = new int[3];
test(mas);
test2(s1);
System.out.println(mas[0]);
System.out.println(s1);
}
static void test(int massive[]) {
massive[0] = 1;
}
static void test2(String string) {
string.concat("111");
}
}
In both cases, for reference types, a copy of the object reference is passed. But only in the case of an array, we change this object. And in the case of a string, a new object is not created in place of a variable (I remember that the string itself cannot be changed).
Why? What do I not understand?
Answer:
The String.concat()
method concatenates the current string with an argument and returns the resulting string. The original string is not modified .
It looks like this schematically
class String {
public String concat(String arg) {
return this + arg;
}
}