Value passing and reference passing in Java

Source: Internet
Author: User

These days have been tangled up this problem, today read this article a bit of thinking, this and C + + function parameters for reference, pointers or a very big difference.

When an object is passed as a parameter to a method, this method can change the properties of the object and return the changed result, is it a value pass or a reference pass?
A: The value is Passed. The Java programming language has only value-passing Parameters. When an object instance is passed as a parameter to a method, the value of the parameter is a copy of the Object's Reference. Point to the same object, the contents of the object can be changed in the called method, but the Object's reference (not the referenced copy) is never Changed.

Java parameters, Whether primitive or reference, are passed as replicas (another argument is a pass value, but a copy is better understood, and the pass value is usually relative to the address).

If the parameter type is the original type, then a copy of the parameter is passed in, that is, the value of the original parameter, which is the same as the value that was previously Discussed.    If you change the value of a copy in a function, the original value is not Changed. If the parameter type is a reference type, then a copy of the reference parameter is passed in, and the copy holds the address of the Parameter. If you do not change the address of the copy in the function, but instead change the value in the address, the changes within the function will affect the parameters passed IN. If you change the address of a copy in a function, such as new, the copy points to a new address, and the passed in parameter points to the original address, so the value of the parameter is not Changed. Reprinted from: http://www.cnblogs.com/clara/archive/2011/09/17/2179493.html

Value passing and reference passing in 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.