Public class B {
Private String s;
Public B (String s) {this. s = s ;}
Public String getS () {return s ;}
Public void setS (String s) {this. s = s ;}
}
Public class {
Private B s;
Public void setS (B s) {this. s = s ;}
Public B getS () {return s ;}
Public void change () {s. setS ("defgh ");}
Public static void main (String [] args ){
B s = new B ("abc ");
B s1 = new B ("aaaaa ");
A AB = new ();
AB. setS (s );
AB. change ();
S1 = AB. getS ();
System. out. println ("s:" + s. getS ());
System. out. println ("s1:" + s1.getS ());
System. out. println ("s. equals (s1):" + s. equals (s1 ));
}
}
The running result is as follows:
However, if Class B is changed to String, s will not change with s1. The running result is as follows:
Why ??
Does String allocate another memory space after being assigned to s in Class a through the form parameter, instead of referencing the same memory space with two variables ??