Java Object Reference passing and value passing

Source: Internet
Author: User

Java Object Reference passing and value passing

@author Ixenos

In Java. Reference Delivery:

In addition to passing arguments to a method (or function) as a "value pass", passing a copy of an object reference is a "reference pass" when assigning a value to a Reference object variable with "=", passing the object's reference to another variable.

  parameter passing , passing a copy of the reference, which appears to be a reference pass, actually passes the copy, which is already the concept of value passing;

  variable assignments , passing references, which are referred to as passing

No reference passing in Java parameter passing is a value pass

1. Objects are never passed in a Java application, and only object references are passed. Therefore, the object is passed by reference. Note the distinction between passing objects by reference and reference passing, but it is important to differentiate how the arguments are passed, which is the intent of the excerpt.

The fact that 2.Java applications pass objects by reference does not imply that Java applications pass parameters by reference . parameters can be object references, while Java applications pass object references by value .

A variable in a 3.Java application can be one of the following two types: a reference type or a base type.

When passed as a parameter to a method, the two types are handled the same way. Both types are passed by value, and no one is passed by reference.

4. passing by value means that when a parameter is passed to a function, the function receives a copy of the original value .

The base type passes a copy of the value,
A reference type passes a copy of the reference.

Therefore, if the function modifies the parameter, only the copy is changed, and the original value remains unchanged.

 Passing by reference means that when a parameter is passed to a function, the function receives the memory address of the original value, which is the copy of the referenced copy instead of the reference variable address value.

Therefore, if the function modifies the parameter, the original value in the calling code changes accordingly.

This is why the reference value is passed and the object's internal method can be called by reference.

Differences in C + + and Java

When a parameter passed to a function is not a reference, it is passed a copy of the value (passed by value). The difference is in the reference.

In C + + when the argument passed to the function is a reference, you pass the reference, or the memory address (passed by reference).

In a Java application, when an object reference is passed to a parameter of a method, you pass a copy of the reference (passed by value), not the reference itself.

Java applications pass all parameters by value, making copies of all parameters, regardless of their type.

Java Object Reference passing and value passing

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.