Are strings passed by reference in Java?

This is a classic java problem. On stackoverflow, many similar questions have been asked, but many answers are wrong or incomplete.

 

This question is easy if you don't want to think deeply. If you want to understand more thoroughly, the question can be confusing.

 

1. An interesting but confusing piece of code


 

public static void main(String[] args) {
  String x = new String("ab");
  change(x);
  System.out.println(x);
}
public static void change(String x) {
    x = "cd";
}

 

output "ab"

 

The C++ code is as follows:

void change(string &x) {
    x = "cd";
}
int main(){    
    string x = "ab";
    change(x);    
    cout << x << endl;
}

 

output "cd"

 

2. Questions that are often elusive


 

x stores a reference to the "ab" string on the heap. Therefore, when x is passed as an argument to the change() method, it is still "ab" in the heap, like this:

 

Because java is pass by value, the value of x is a reference to "ab". When the method change() is called, it creates a new string object "cd", and then x points to "cd", as shown:

This seems like a very reasonable explanation. They are well aware that java is pass by value, but what's wrong here?

 

3. What exactly does this code do?


 

There are several errors in the above explanation. In order to make it easier to understand, we'd better briefly go through the whole process.

When the string "cd" is created, java allocates the amount of memory needed to store the string. Then, the object is assigned to the variable x, in effect assigning a reference to the object to the variable x. This reference is the memory address where the object is stored.

 

The variable x contains a reference to a string object, x is not the string object itself. It is a variable that stores a reference to the string object 'ab'.

 

java is pass by value. When x is passed to the change() method, it is actually a copy of the value of x (a reference). After the method change is called, another object "cd" is created with a different reference. The value of the local variable x inside the method becomes a reference to "cd". What is changed here is the reference value of the local variable in the method, not the original quoted string "ab".

Look at the picture:

 

 

4. Wrong explanation:


 

The problem raised from the first code snippet has nothing to do with string immutability. Even if String is replaced by StringBuilder, the result is still the same. The point is that variables store object references, not the objects themselves!

 

5. Fix the problem


 

If we really need to change the value of the object, first of all, the object should be mutable, such as StringBuilder. Second, we need to make sure that no new object is created and assigned to the parameter variable, since Java just passes by value.

public static void main(String[] args) {
  StringBuilder x = new StringBuilder("ab");
  change(x);
  System.out.println(x);
}
public static void change(StringBuilder x) {
  x.delete(0, 2).append("cd");
}

 

Author: Under the thief
. Link: https://www.jianshu.com/p/df903886b5a9
Source: Jianshu

Follow our "java union" to get more java technology dry goods, waiting for you to tease!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325246893&siteId=291194637