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 instead of a variable, a new object is not created (I remember that the string itself cannot be changed).

Why? What don't I understand?

  • one
    String.concat does not change anything, it returns a new line - vp_arth

3 answers 3

The String.concat() method concatenates the current string with an argument and returns the resulting string. The original string is not modified .

It looks so schematic

 class String { public String concat(String arg) { return this + arg; } } 
  • @Regent you are right. Up to date - Anton Shchyrov

The link to the string itself is passed by value, that is, a copy of the argument link is created. The function works with a copy of the link.

 string.concat("111"); 

However, the original link does not change. Moreover, this method does not change the original string. It creates a new line. So in any case, the original string does not change even if you write in the function

 string = string.concat("111"); 

You can present the definition of a method and its call as follows.

 test2(s1); // ... static void test2( /* String string */) { String string = s1; string = string.concat("111"); } 

In the case of an array, the memory occupied by the object referenced when using the indexing operator changes.

  • Those. Without adding a "return" string object in the link of the original method can not be changed? - BruceVVayne
  • @chakki can be changed, but in practice this is definitely not worth doing. Yes, and even think about it is not worth it. - Regent

String.concat does not change anything, it returns a new string.

In other answers this topic is revealed, I also wanted to add the following.
Java strings are not mutable. This, among other things, guarantees you that declared as String a = "string"; The string cannot be changed by anyone to whom you transmit it.
Other classes / methods may create new lines based on this, but not able to change yours.
As long as you yourself do not replace the value of a with a new value ( a = getNewStr(); ), you can be sure that it does not change.
Even replacing the value in this way, you do not change the original string, but only change the link to the newly created one. If the pointers to the old value are somewhere ( String b = a; ), they are safe.


All the primitive wrapper classes ( Integer , Long and so on) have this property ( immutable ).

  • Generally speaking, this is not entirely true. The situation in which the code String string = "abc"; changeString(string); System.out.println(string); String string = "abc"; changeString(string); System.out.println(string); will lead to the output on the screen, for example, 0bc , is very real. Another thing is that this is a series of "do not try to repeat it at home." - Regent
  • The methods are the same as for поменять значение константы ? They are better not to know. - vp_arth
  • Yes, all the same. Yes, you probably shouldn't teach people bad things. - Regent