2013年9月25日星期三

Why one did not change , a change


public class A {


public static void main(String[] args) {

String a="af";
TE te=new TE();
BB bb=new BB();
te.test(a);
te.test1(bb);
System.out.println(a);
System.out.println(bb.getB());

}


}
class TE{
public String test(String a){
a="1";
return a;
}
public String test1(BB bb){
String a="1";
bb.setB(a);
return a;
}
}
class BB{
private String  a;
private String b;
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}

}



------ Solution ------------------------------------- -------
java is passed by value.
http://bbs.csdn.net/topics/90058535
------ For reference only --------------------- ------------------
a = te.test (a);
------ For reference only -------- -------------------------------
upstairs was right , test methods have not changed so you do not have to receive , test1 change is referenced inside the property so changed


------ For reference only ---------------------------------- -----
1 floor right
two different memory addresses pointing
------ For reference only --------------------------- ------------


------ For reference only ---------------------------------------
a output must be af, as a reference point has not changed , upstairs solution
------ For reference only -------------------------- -------------
you directly
System.out.println (a);
System.out.println (te.test (a));
to know why
------ For reference only ------------------------------ ---------
you just performed for a TE in the test (String a) method, but did not put value back to a, the output is directly in front of the definition of a

没有评论:

发表评论