Java technology _java Thousands of questions (0039) _ What is the difference between a reference pass and a value pass

Source: Internet
Author: User

Click to enter _ more _java thousand ask

1. What is value passing

Value delivery is the transfer of a value stored in a storage unit in memory space to another storage unit. (storage units in Java are not addresses of physical memory, but have dependencies)
For example:

//定义了一个改变参数值的函数publicstaticvoidchangeValue(int x) {??x = x *2;}publicclass TestMain{//调用该函数int5;System.out.println(num);changeValue(num);System.out.println(num);}

The results are as follows:
5
5

The value of NUM before and after the call function ChangeValue () has not changed. Specific process

    1. When NUM is passed as a parameter to the changevalue (int x) method, first allocates a storage unit for the x variable in memory space (we say x points to the storage unit).
    2. The value ("5") that is stored in the storage unit that NUM points to in memory space is passed to the parameter variable ("x") in ChangeValue (int x), which means "5" is passed to the storage unit to which the x variable points.
    3. All operations on the x variable in the changevalue (int x) method are for the storage unit that the X points to. There is no relationship with the storage unit that Num points to, and of course it does not change the value in this storage unit.

Therefore, the value is passed, passing the contents of the storage unit (8 basic types: value, non-basic type: The address of the actual object).

2. What is reference delivery

only values are passed in Java, and no reference is passed.
The so-called reference pass is just a faulty concept.
For example:

Class Person { Public StaticString name ="Jack";//Define a method to change object properties Public Static void ChangeName(Person P) { ?? P.name ="Rose"; } Public Static void Main(string[] args) {//Define a Person object, which is a reference to this objectPerson person =NewPerson ();//Display the Name property of this object firstSystem. out. println (Person.name);//Call ChangeName (Person p) methodChangeName (person);//Display the Name property of this object again to see if it has changedSystem. out. println (Person.name); } }

Results after execution:
Jack
Rose

From the result, the method uses an object parameter, and the manipulation parameters can change the incoming object. Our misconception about the passing of references is that:
The object copies a reference copy to the parameters of the calling method, allowing the method to manipulate the object.
This idea is a mistake that beginners often make.

Actually the process

    1. The main method is new to the object person, and the storage space is actually assigned two objects: the new entity object that creates the person class, and the person that refers to the object.
      Where entity objects reside in heap memory, reference variables are stored in stack memory (Java storage features).
      Read more about Java storage here:
    2. The reference variable person points to the stack memory, which holds the logical address of the entity object in the heap.
    3. Call the ChangeName (person p) method, passing the person reference variable to the method parameter P (passed by value, passing: The logical address of the entity object). At this point, the operation of p in the ChangeName method has no relation to the person.
    4. In the ChangeName method, an entity object that points to a value in the storage unit that P points to (that is, the logical address of the entity object). The entity object was changed directly.
    5. Because the value in the storage unit pointed to by person is also the logical address of the entity object, the entity object has been changed in the 4th step. So there are the above results.
3. What is the difference between a reference pass and a value pass?

Reference passing is a pseudo-concept, andonly values are passed in Java .

Java technology _java Thousands of questions (0039) _ What is the difference between a reference pass and a value pass

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.