This is a classic problem in Java, there are a lot of similar problems on Stack-overflow, but many of the answers are wrong or incomplete; If you don't think about it, you'll think it's a simple question, but when you go deeper, you'll find that the problem is easy to confuse.
An interesting and confusing code
Package Simplejava; Public class Q14 { publicstaticvoid Change (String x) { = "CD"; } Public Static void Main (string[] args) { new String ("AB"); Change (x); SYSTEM.OUT.PRINTLN (x); }}
Results Print:
Ab
The C + + version is as follows:
void Change (string &x) { "cd";}
int Main () { string"ab"; Change (x); << x << Endl;}
Printing results:
Common Mistakes in CD understanding
The x variable stores a reference to the "AB" object in the heap, and when X is passed inside the change() method as a parameter, it still points to the "AB" object in the heap as follows:
Because Java is passed by value, the value of x is a reference to the "AB" object, and when the method change() is called, a new object "CD" is created, and then x points to the "CD" object, as shown in:
This looks like a perfect explanation, and they know that Java is always passed by value. But where is the problem?
What the hell is this code doing?
There are a number of errors in the above explanation, so we should clear the whole process before we can understand the problem more easily.
When the string object "AB" is created, Java allocates the corresponding size of memory space, and then the object is assigned to the variable x, in fact, the x variable is stored in the object reference, this reference is "AB" object in memory address;
The x variable contains a reference to the object, X is not an "AB" object, but a variable that stores the "AB" Object reference (memory address) .
Java is passed by value, and when X is passed into the change () method, the fact is that a copy of the x variable is passed in. Another object, "CD", is created inside the method change (), and it has a different reference. The real change is the copy of the x variable whose value becomes a reference to the "CD" Object instead of the original x variable being changed;
Note: I feel a little confused, I understand , first of all, say this parameter x, which is equivalent to a local variable, when using this parameter , will be assigned a new storage location, copy the argument to the location, and the The copy value is passed to the method;
inside the Change () method, execute the x = "cd", where x is actually a copy of the x variable of the main method, at first it holds a reference to the "AB object", and after executing the code, its value becomes " CD object, and the x variable of the main method does not change, and the reference to the "AB" object is still stored.
Another explanation for the error
The cause of this problem is not related to the non-deformation of the string, even if you replace the string with a StringBuilder object, the result remains the same, the key point is that the variable stores the object's reference, not the object itself;
the way to solve this problem
If you really want to change the value of this object,
First of all, this object is to be changed, for example StringBuilder.
Second, we want to make sure that no new objects are created to be assigned to parameter variables, because Java can only be passed by value.
The following code:
Public Static void Main (string[] args) { new StringBuilder ("AB"); Change (x); SYSTEM.OUT.PRINTLN (x);}
Public Static void Change (StringBuilder x) { x.delete (0, 2). Append ("CD");}
Link: http://www.programcreek.com/2013/09/string-is-passed-by-reference-in-java/
The "simple Java" string is passed by "reference"