Transferred from: http://www.cnblogs.com/linkstar/p/5951141.html
public class Example {
String teststring = new String ("good");
Char[] Testchararray = {' A ', ' B ', ' C '};
public static void Main (string[] args) {
Example ex = new Example ();
Ex.change (Ex.teststring,ex.testchararray);
System.out.println (ex.teststring);
System.out.println (Ex.testchararray);
}
public void Change (String teststring,char[] testchararray) {
teststring = "Hhhhhh";
Testchararray[0] = ' W ';
}
}
What is the last output of this code?
Give your answer after consideration.
+ View Code
Goodwbc
The first thing to understand is what is the Java approach passed on?
For a base type, the value of the base type is passed, and the address is passed for the reference type.
So whatever it is, the value is passed, because you can interpret the address as 9x0000, which is also a value.
So all that is passed is the value.
When does the method change its original value, and when does it not?
1, as long as the basic type, pass the value, this value is copied a copy out, so how will not change.
2, the reference type, passes the address, if this address has changed, then the original value must be unchanged.
3, the reference type, passes the address, if the address does not change, and changes the address corresponding to the object's properties, so long will change the original value.
If you do not understand, then simply, as long as the incoming object in the method to re-assign a new object, then the original is not become.
As in the method string, re-assigned a string, this time the address and changed, so the original unchanged.
The char array, instead of re-copying a new char array, changes the elements in the char array, which changes the original value.
For the memory analysis, the following blog is particularly clear, I will not say more.
It's really important to understand this.
Http://www.cnblogs.com/wangzhongqiu/p/6602934.html
"Java Fundamentals" 11, Java methods only value is passed, no reference is passed