2013年9月20日星期五

java face questions , seek guidance

Great God

public class TestStr {
public String str="1234";
public Integer number=10;

public void changeStr(String s){
s="hello";
}
public void changeInt(Integer i){
i=20;
}

public static void main(String[] arg0) {
TestStr test = new TestStr();
test.changeStr(test.str);
test.changeInt(test.number);
System.out.println(test.str);
System.out.println(test.number);
}

}


output is:
1234
10
Why is this ? Why changeStr can not change the value of str , very puzzled . .
------ Solution ---------------------------------------- ----
can say specifically what     

java in method calls , arguments passed to its value corresponding formal parameter , method execution parameter value change does not affect the value of the argument .
------ Solution ---------------------------------------- ----
Java 's value is passed , the method parameters and arguments occupy different memory space, method invocation , the arguments to the method copies the value to the parameter , the parameter in the method for operating without will affect the argument . . . . Overall in JAVA method can not be modified to achieve change in the value of the formal parameter method arguments worthy purpose
------ For reference only ------------------ ---------------------
 value transfer problems
------ For reference only ---------------------- -----------------
can say specifically what
------ For reference only ---------------------------------------
Oh, and understood it. Extremely grateful ! !
------ For reference only -------------------------------------- -

understand is that the parameter is actually a copy of the argument is not it ?         
------ For reference only ---------------------------------- -----
further questions, in C or C + + it is allowed to do this ?
------ For reference only -------------------------------------- -
c is not object-oriented
------ For reference only ------------------------------ ---------
c + + have passed by reference

没有评论:

发表评论