Value passing and reference passing for Java

Source: Internet
Author: User

Today, there is a bug, because of this cause. Alas, there is a lack of AH, continue to work

https://www.zhihu.com/question/31203609/answer/50992895

First, do not dwell on the literal meaning of pass by Value and pass by Reference, otherwise it is easy to fall into the so-called "all references in essence is the value of the transfer" This does not solve the problem of meaningless controversy.
What's more, if you want to know whether Java is a value or a reference, at least you have to know the value of the transfer and the exact meaning of the reference it? But if you already know the exact meaning of these two names, then you can judge whether Java is a pass or a reference.
It's like using a college term to explain a high school topic that doesn't make any sense to beginners.

One: Figure out the difference between the basic type and the reference type
int num = 10;String str = "hello";

<img src= "https://pic2.zhimg.com/166032bc90958c21604110441ad03f45_b.jpg" data-rawwidth= "728" data-rawheight= "458" class= "Origin_image zh-lightbox-thumb" width= "728" data-original= "https://pic2.zhimg.com/ 166032bc90958c21604110441ad03f45_r.jpg ">,num is the basic type, and the value is stored directly in the variable. While STR is a reference type, the variable holds only the address of the actual object. This variable is generally referred to as a "reference", a reference to the actual object, and the contents stored in the actual object. , num is the base type, and the value is stored directly in the variable. While STR is a reference type, the variable holds only the address of the actual object. This variable is generally referred to as a "reference", a reference to the actual object, and the contents stored in the actual object.

Two: Figure out the function of the assignment operator (=)
num = 20;str = "java";
<img src= "https://pic3.zhimg.com/287c0efbb179638cf4cf27cbfdf3e746_b.jpg" data-rawwidth= "714" data-rawheight= "572" class= "Origin_image zh-lightbox-thumb" width= "714" data-original= "https://pic3.zhimg.com/ 287c0efbb179638cf4cf27cbfdf3e746_r.jpg "> for basic type num, the assignment operator changes the value of the variable directly, and the original value is overwritten. For the base type num, the assignment operator changes the value of the variable directly, and the original value is overwritten.
For reference type STR, the assignment operator changes the address saved in the reference, and the original address is overwritten. but the original object will not be changed (important).
As shown, the "Hello" string object has not been changed. (objects that are not pointed to by any reference are garbage and are recycled by the garbage collector)

Three: What happens when a method is called?
第一个例子:基本类型void foo(int value) {    value = 100;}foo(num); // num 没有被改变第二个例子:没有提供改变自身方法的引用类型void foo(String text) {    text = "windows";}foo(str); // str 也没有被改变第三个例子:提供了改变自身方法的引用类型StringBuilder sb = new StringBuilder("iphone");void foo(StringBuilder builder) {    builder.append("4");}foo(sb); // sb 被改变了,变成了"iphone4"。第四个例子:提供了改变自身方法的引用类型,但是不使用,而是使用赋值运算符。StringBuilder sb = new StringBuilder("iphone");void foo(StringBuilder builder) {    builder = new StringBuilder("ipad");}foo(sb); // sb 没有被改变,还是 "iphone"。

Focus on understanding why, the third example and the fourth example results in a different result?

The following is a schematic of the third example:
<img src= "https://pic4.zhimg.com/d8b82e07ea21375ca6b300f9162aa95f_b.jpg" data-rawwidth= "772" data-rawheight= "398" class= "Origin_image zh-lightbox-thumb" width= "772" data-original= "https://pic4.zhimg.com/ D8b82e07ea21375ca6b300f9162aa95f_r.jpg ">builder.append (" 4 ") Builder.append ("4")
<img src= "https://pic4.zhimg.com/ff2ede9c6c55568d42425561f25a0fd7_b.jpg" data-rawwidth= "696" data-rawheight= "424" class= "Origin_image zh-lightbox-thumb" width= "696" data-original= "https://pic4.zhimg.com/ Ff2ede9c6c55568d42425561f25a0fd7_r.jpg "> Here is a diagram of the fourth example: Here is a diagram of the fourth example:
<img src= "https://pic4.zhimg.com/d8b82e07ea21375ca6b300f9162aa95f_b.jpg" data-rawwidth= "772" data-rawheight= "398" class= "Origin_image zh-lightbox-thumb" width= "772" data-original= "https://pic4.zhimg.com/ D8b82e07ea21375ca6b300f9162aa95f_r.jpg ">
Builder = new StringBuilder ("ipad"); After
<img src= "https://pic2.zhimg.com/46fa5f10cc135a3ca087dae35a5211bd_b.jpg" data-rawwidth= "710" data-rawheight= "438" class= "Origin_image zh-lightbox-thumb" width= "710" data-original= "https://pic2.zhimg.com/ 46fa5f10cc135a3ca087dae35a5211bd_r.jpg ">

Value passing and reference passing for Java

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.