To explain the value passing in Java using the method of drawing puppies

Source: Internet
Author: User

Before I start looking at the puppy, let's look at a very simple topic:

What is the output of the following program?

New Dog ("Mong Choi"); ChangeName (Mydog); System.out.println (Mydog.getname ());  Public void ChangeName (dog) {    dog.setname ("Xiao Qiang");}

If your answer is "Xiao Qiang", well, congratulations on your correct answers. Let's Change the code:

New Dog ("Mong Choi"); ChangeName (Mydog); System.out.println (Mydog.getname ());  Public void ChangeName (dog) {   new  Dog ();   Dog.setname ("Xiao Qiang");}

Yes, I just added a code to the ChangeName method.

New Dog ();

What is the output of this time?

    • A Wang Choi
    • b Cockroach

The answer is a Wang Choi, the ChangeName method did not change the name of Mydog. If you answer wrong, it's OK, I will start to draw the puppy, you can understand the painting, if you answer correctly, but do not understand the reason, then I draw the puppy will certainly be able to help you.

What is Mydog?

First you have to understand, what is the variable Mydog in the code? Is Mydog really a dog? No! No! Mydog is just a dog-walking leash!

In other words, Mydog is not new out of the heap objects (object)! Mydog is just a reference to this object instance (reference)! If you know enough about the Java Runtime data region, you should be aware that this reference is placed on the virtual machine stack.

Parameter passing

Now you know, Mydog is just a rope, but that doesn't seem to explain why the ChangeName method didn't change the name of Mydog to "Xiao Qiang" because, according to the existing understanding, dog = new dog is tying my dog rope to another puppy. And then named the puppy "Xiao Qiang", just like this:

But the truth is, Mydog is still called Wong Choy, which is why?
The problem is on the method call, when I execute ChangeName (mydog) This line of code, Mydog the dog rope, was copied a copy, and passed into the ChangeName method of the dog, is the copy of the article, like this:

Then execute dog= new Dog (), this line of code, is the copy of the dog rope, from the Mydog to bind, re-tied to the new puppy, which was later named "Xiao Qiang" puppy:

And Mydog still tied to Wong choy body, this also explains, why execute the method out, Mydog.getname () or Wang Choi. And in the first code, we did not execute dog= new dog (), also did not change the dog tied to the puppy, dog or tied to wong choy body, so dog.setname ("Xiao Qiang") on the name of the prosperous wealth to the cockroach.

Example of string

String str = "AAA"; changestring (str); System.out.println (str);  Public void changestring (String str) {    = "BBB";}

If you understand the example above, then it should not be difficult to understand, changestring method, just the new copy of the reference Str, pointing to another string constant object "BBB", the method body outside the STR is not affected, or point to the string constant "AAA", So the final print is AAA.

Examples of int

int i = 1publicvoid changeint (int= 2;}

For basic data types, they do not have references, but do not forget that when the function is called, the copied action will still be done, when executing changeint (i), I will be copied to a new int, passed to the Changeint method, so no matter what the changeint internal to the parameter, The outside I will not be affected. The final print is still 1.

Value passing and reference passing

The above mentioned parameters in the transfer process of the copy operation, plainly, is the = operation. Take the int example above and do a method inline, which is actually the case:

int i = 1; // method Inline, equivalent to executing the Changeint method int // Create a new variable with the same as I // Modify the value of J, I do not change System.out.println (i);

For the base data type, the = operation copies the right variable (r_value) to the left variable (l_value), and to the object, to be exact, it should be a reference to the object (as described above Mydog), and the = operation also copies the right reference completely to the left reference, Both point to the same object instance.
This = operation, which is the fundamental difference between value passing and reference passing, also leads to the following visual differences in value passing and reference passing:

    • If the argument is a value pass, then the caller (outside of the method body) and the callee (method body) are using two different variables, and the changes in the method body face variables do not affect variables outside the method body. The reason that Java can change the object outside of the method body inside a method is because the object is referenced within the body of the method, but the reference is two different references to the outside of the method body, and the reference within the method body points to another object and does not cause a reference outside the method body to point to another object.
    • If the argument is a reference pass, then the caller (outside the method body) and the callee (inside the method body) are using two identical variables, and the changes in the method body that face the variable affect the variables outside the method body.

Java variables are not objects

In the above explanation, you also know a very important point: the variables in Java, either the basic data type, or the reference type to the object instance (dog rope), is definitely not an object (dog).

To explain the value passing in Java using the method of drawing puppies

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.