V. String in Java is a "reference"

Source: Internet
Author: User

This is the classic Java problem. Many similar questions have been asked in StackOverflow, with many incorrect or incomplete answers. If you don't want too much, you'll think the problem is simple. (The question is easy if you don't think too much.) If you want more, it will be very confusing to you.

1. The following code snippet is interesting and confusing

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";}


The "AB" will be printed.
In C + +, the code is as follows:

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


It will print "CD".
2. Problems of common perplexity
X stores a reference to the string "AB" in the heap. When x is used as the change () method parameter, it still points to "AB" in the heap such as:

Because JVA is a value, X is a reference to "AB". When the method change () is called,
It creates a new "CD" Object, and X now points to "CD" as follows:

This looks like a very good explanation, and they clearly describe the passing of values in Java.
But what's wrong here?


3, how does the code actually do?
There are several errors explained above. It's a good idea to simply keep track of the whole process for ease of understanding.
When the string "AB" is created, Java allocates a large amount of memory to present the string object. Therefore, this object is assigned to the variable x, which is actually assigned a reference to the object. A reference is a memory address in which this object exists.

Variable x stores a reference to a string object. But X is not itself not a reference. It is a variable (memory address) that stores references.
Java only passes values. When X is passed to the change () method, a copy of the X value (a reference) is passed. When the change () method creates another "CD" Object, it has a different reference. The variable x changes its reference (pointing to "CD") instead of referencing itself.
Shows what actually happened.

4, the wrong explanation
The problem with the first code fragment is independent of the immutability of the string. Even if you replace string with StringBuilder, the result remains the same. The key is that the variable stores the reference, but not the reference itself.
5, the solution of this problem
If we really need to change the value of the object. First the object must be able to be changed, such as StringBuilder. Second, we need to make sure that no new objects are created and copied to the parameter variables because Java is only value-passed.

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");}

V. String in Java is a "reference"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.