In Java, there is no reference transfer and only value transfer. Right?

Source: Internet
Author: User

Let's look at the Java job search book. There is one such thing: in Java, there is no reference transfer, and only value transfer. I don't think so. I checked the information online.

 

A copy of the reference is actually copied during the transfer of the reference.
A A = new ();
Test ()
Equivalent
(A B =;
Test (B)
)

If the data type is basic, modifying this value does not affect the variable passed in as a parameter, Because you modify the local variable of the method and it is a copy.
If an object is referenced, it is also the same as a copy, but the copy and the reference passed as a parameter point to the same object in the memory, so you can also operate on that object through this copy. However, if you modify the reference itself, such as directing it to another object in the memory, the reference originally passed as a parameter will not be affected.

I think it's okay to understand this. It doesn't matter whether the value is transferred or referenced, but the value transfer is more suitable. This value can be a reference or a basic data type.

--------- Reference others' words

It is true that all calls are passed by value, because the call stack principle limits that we can only press various values into the stack, and when the method returns, instead of processing the values in the stack, simply adjusting the stack top pointer will discard the values originally pushed into the stack. Therefore, all the parameters passed to the method body for the pressure stack, the modifications made inside the method are invisible to the outside world. In view of this situation, in order to bring the parameter modification of the function to the function, different languages have done different processing, and pointers can be passed in C/C ++, by default, Java transmits object references. If the landlord has to call the method to press the stack address as a value, it is actually not impossible, but I think this is a little far-fetched.

--------- Reference others' words

Agree with the two comments! Thank you.

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.