Java string problem

the question may be dementia, but I haven"t found it for a long time. Look for the answer-_ | |

String A = "aaa";
String B = A + "bbb";

A = "AAA";

System.print(B);

I want the output to be "AAAbbb" instead of "aaabbb"

ask for advice


B where a new object has been created has nothing to do with A


String A = "aaa". Such a string is stored in the constant pool of characters in the method area. The situation described by the subject occurs in the reference type. You can try String A = new String ("aaa");
B = A;
A = new String ("AAA");
putting a string into heap memory in this way may lead to a description of the subject.


 

the earliest answer has already mentioned that there is no correlation between your An and B after the change.
String A = "aaa";
String B = A + "bbb";

A = "AAA";
B = A + "bbb"; / / at least this

System.out.print (B);

Menu