A pen question to straighten out value passing and reference passing in Java

Source: Internet
Author: User
Tags call by reference stringbuffer

The previous time took part in an interview, in which there was a reference to the problem, because it was not considered clearly so wrong.  
Now, the title is as follows:

private static void Change (StringBuffer str11, StringBuffer str12) {

Str12 = Str11;
Str11 = new StringBuffer ("New World");
Str12.append ("New World");
}

public static void Main (string[] args) {
StringBuffer str1 = new StringBuffer ("good");
StringBuffer str2 = new StringBuffer ("bad");
Change (str1, str2);
System.out.println (Str1.tostring ());
System.out.println (Str2.tostring ());
}

Is this a very simple topic, you should know the answer? The answer is:

Good new world  
AaG

The following is the answer to the question why these two answers, the following will be done detailed graphic analysis.  
To understand this problem, you need to understand that value passing and reference passing in Java.

    • value invocation (call by value): During parameter passing, formal parameters and arguments occupy two completely different memory spaces. The content stored by the formal parameter is a copy of the contents of the argument store. In fact, the delivery of a Java object conforms to this definition, except that the contents of the parameters and arguments are not the values of the variables in the general sense, but the addresses of the variables. Well, think back: The address of the variable is not a value!

    • reference calls (call by Reference): In the process of parameter passing, formal parameters and arguments are exactly the same chunk of memory space, both equals. In fact, formal parameter names and argument names are just different symbols in programming, and in the process of running the program, the space stored in memory is the most important. Different variable names do not indicate that memory storage space is used differently.

Then let's draw an analysis of the questions thrown at the beginning of the article:
Put the code here again, you can compare the code with the graph to see.

private static void Change (StringBuffer str11, StringBuffer str12) {

Str12 = STR11;//2
Str11 = new StringBuffer ("New World");//3
Str12.append ("New World");//4
}//5

public static void Main (string[] args) {
StringBuffer str1 = new StringBuffer ("good");
StringBuffer str2 = new StringBuffer ("bad");
Change (str1, str2);//1
System.out.println (Str1.tostring ());
System.out.println (Str2.tostring ());
}

  • Perform the diagram to the first step :

  • Take the diagram to the second step, where the formal parameter Str11,str22 in the Chage method is the address copy of the argument str1,str2.  

  • Here Str11 and Str22 are the address copies of the argument STR1,STR2, followed by the third step of the diagram:

  • The Str12 address points to "good", followed by the fourth step diagram :

  • The address in Str11 points to the new object in the heap, "New World", followed by the fifth step of the diagram:

Here the picture is finished, really not very good at drawing, we first reluctantly see it.  
Here is a very important point is stringbuffer is variable, specific explanation I found an article: StringBuffer invariance

Because here Str12.append ("New World") directly modifies the value of the good. So the main function will print: Good New World.

Even after the end of the story, a very simple example, here is a copy of the previous question of the disk, there is no place to welcome you to correct.

A pen question to straighten out value passing and reference passing in Java

Related Article

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.