Value passing and reference passing | References and pointers

Source: Internet
Author: User
Value passing: (formal parameter type is the basic data type): When the method is called, the actual parameter passes its value to the corresponding formal parameter, the formal parameter only initializes its own storage unit content with the value of the actual parameter, it is two different storage units, so the change of the formal parameter value in the method execution does not affect the value of the actual parameter.

A reference pass (a formal parameter type is a reference data type parameter): It can also be called an address pass. Method invocation, the actual parameter is an object (or an array), then the actual parameters and formal parameters point to the same address, in the execution of the method, the operation of the formal parameters is actually the operation of the actual parameters, the result is preserved after the end of the method, so the change in the method execution parameters will affect the actual parameters.

Pointers and references look completely different (pointers are used with the operator "*" and "--", reference using the operator ". "), but
Is that they seem to have the same function. Pointers and references let you refer to other objects indirectly. How do you decide at what time
Use pointers, when to use references.
First, realize that you cannot use a reference to a null value under any circumstances. A reference must always point to some
Object. So if you use a variable and point it to an object, the variable may not point to the
Any object, you should declare the variable as a pointer, because you can assign a null value to the variable. Conversely, if you change
Amount must point to an object, such as your design does not allow the variable to be empty, then you can declare the variable as 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.