In Java, the pass of a function parameter is a value pass or a reference pass

Source: Internet
Author: User

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.

It can be understood that there is a person (name= "S"), passed as a parameter to a method, in which the person is reset name= "B", then a conclusion

① This person is still the man, it has always been him, but his name has been changed, whether it is the original reference or the reference in the method, the reference is an object, so the value of all references have changed

② If a person is re-assigned to a parameter in the method, then this parameter has no half-penny relationship with the original person (note string sb= "SB" is also a new string)

The Java programming language has only the value pass parameter. When an object instance is passed as a parameter to a method, the value of the parameter is the "reference" of the object to a "Vice". Pointing to the same object, the object's contents 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, pass "copy" (another is "pass value"), but a copy is better understood, and the value of the pass 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.

In Java, the pass of a function parameter is a value pass or a reference pass

Related Article

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.