Java is only passed by value, not by reference! (two parameter cases are value passing)

Source: Internet
Author: User

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   
    1. ... ...
    2. Defines a function that alters the value of a parameter
    3. Public static void ChangeValue (int x) {
    4. x = x *2;
    5. }
    6. ... ...
    7. Call this function
    8. int num = 5;
    9. SYSTEM.OUT.PRINTLN (num);
    10. ChangeValue (num);
    11. SYSTEM.OUT.PRINTLN (num);
    12. ... ...

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
  1. ... ...
  2. Class Person {
  3. Public static String name = "Jack";
  4. ... ...
  5. }
  6. ... ...
  7. Define a method that alters the properties of an object
  8. Public static void ChangeName (person p) {
  9. P.name = "Rose";
  10. }
  11. ... ...
  12. Public static void Main (string[] args) {
  13. Defines a person object, which is a reference to this object
  14. Person person = new Person ();
  15. The Name property of this object is displayed first
  16. System.out.println (Person.name);
  17. Call the ChangeName (person p) method
  18. ChangeName (person);
  19. Display the Name property of this object again to see if it has changed
  20. System.out.println (Person.name);
  21. }

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)

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.