Java value passing or reference passing doubts

Source: Internet
Author: User

the transfer of the parameters in Java is inherently only a value pass, no matter what you preach, the pass is just a copy, and this copy is stored as a local variable of the method in the stack.

1. Assuming that the parameter type is the basic data type, changing the value does not affect the variable that is passed in as a parameter, because you are changing the local variable of the method, which is a copy.

2, it is assumed that a reference to an object, but also the same, but also a copy, but this copy and as a reference to pass in the references to the same object in memory, so you can also manipulate the object through this copy. But suppose you change the reference itself, for example, to point to another object in memory, and the reference that was passed in as a parameter is not affected.

The most confusing thing here is that when you pass a reference to an object, the value of that parameter is changed in the sub-method, causing it to change and therefore mistakenly assumed to be a reference pass. In the definition of C + +. so-called Reference Delivery refers to passing the address of the actual parameter to the function when the function is called. Then the changes to the parameters in the function. will affect the actual number of references. However, in Java is not satisfied, first of all, Java is passing a copy of the object instead of the object itself, and secondly, when you specify the memory address in the function, the actual number of the parameters is not affected. This is completely different from C + +, so it's not a reference pass, it's just an effect like a reference pass.

Java values Pass or reference pass-through doubts

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.