Today, in an interview book, I saw an issue with a parameter passing in Java:
Writes whether the object in Java is passed as a parameter to a method, is it a value pass, or a reference pass?
I have no doubt answered: "Quote pass!" , and also feel very familiar with this feature of Java!
Turns out, I was wrong!
The answer is:
Value Pass! Java is only passed by value, not by reference!
When I came home, I couldn't wait to inquire about the problem, and felt that I had made a mistake about the basic problem of Java.
Comprehensive online description, I probably understand what is going on, and now tidy up as follows, if there is no place to look at the great God proposed!
Let's start with an example of a value pass that is familiar to programmers:
Java code
- ... ...
- Defines a function that alters the value of a parameter
- Public static void ChangeValue (int x) {
- x = x *2;
- }
- ... ...
- Call this function
- int num = 5;
- SYSTEM.OUT.PRINTLN (num);
- ChangeValue (num);
- SYSTEM.OUT.PRINTLN (num);
- ... ...
The answer is obvious: the value of num before and after the call function ChangeValue () has not changed.
To do a primer, I use a chart to depict a value-passing process:
When NUM is passed as a parameter to the ChangeValue () method, it is the value stored in the storage unit that NUM points to in memory space, which is "5", which is transmitted to the X variable in the ChangeValue () method. This x variable also allocates a storage unit in the memory space, at which point the value of NUM 5 is transferred to the storage unit. Thereafter, all operations on X in the ChangeValue () method are for the storage unit pointed to by X, which is not related to the storage unit that NUM points to!
Naturally, after a function call, the value of the storage unit that NUM points to has not changed, which is called "Value passing"! The essence of the value Pass is to pass the contents of the storage unit, not the address or reference!
Next, let's look at how the object parameters in Java are passed:
Also, give a piece of code first:
Java code
- ... ...
- Class Person {
- Public static String name = "Jack";
- ... ...
- }
- ... ...
- Define a method that alters the properties of an object
- Public static void ChangeName (person p) {
- P.name = "Rose";
- }
- ... ...
- Public static void Main (string[] args) {
- Defines a person object, which is a reference to this object
- Person person = new Person ();
- The Name property of this object is displayed first
- System.out.println (Person.name);
- Call the ChangeName (person p) method
- ChangeName (person);
- Display the Name property of this object again to see if it has changed
- System.out.println (Person.name);
- }
The answer should be clear to everyone:
First time: "Jack"
Second display: "Rose"
Method uses an object parameter, the contents of the object can be changed, I have always believed that the object should be copied a reference copy to the calling function parameters, so that the method can operate on this object, is actually wrong!
Http://www.cnblogs.com/clara/archive/2011/09/17/2179493.html writes that 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.
Why is this "value passing" instead of "reference passing"?
I'm still using a graph to illustrate the comparison:
The main function new has an object person, which is actually assigned two objects: The entity object of the newly created Person class, and the person that refers to the object.
Note: In Java, the newly created entity object opens up space in the heap memory, while the reference variable opens up space in the stack memory.
As shown, the left side is the heap space used to allocate memory to the newly created entity object, the red box is the entity object of the new person class, 000012 is the starting address of the entity object, and the right side is the stack space, which is used to allocate memory for reference variables and some temporary variables. Reference to the new entity object in which the person can see that the contents of its storage unit is 000012, which is the starting address of the new Person class entity object, which means that it points to the entity object.
This time, the show came on stage:
The ChangeName () method is called, and the person is passed into the method as an object parameter, but we pay special attention to what it is passing in!!! The person reference variable passes the contents of its own storage unit to the P-variable of the ChangeName () Method! That is, the address of the entity object is passed to the P variable, and since then, all operations on p in the ChangeName () method are directed at the storage unit that P points to, and it is no longer related to the storage unit that the person references the variable to!
Recalling the above example of a value pass, the value of the pass, is to pass the contents of the storage unit to the calling function of the parameter, here is not the same, is called "value passing", rather than "reference delivery"!!!
So why is it possible for the object to change inside?
That's because the content in the storage unit that P points to is the address of the entity object, so that p also points to the entity object, so you can change the properties inside the Object!
This is the ultimate reason most of us would mistakenly think of as "Citation Pass"!!!
Java is only passed by value, not by reference! (two parameter cases are value passing)