"StackOverflow Good question" Java parameters are passed by value or by reference

Source: Internet
Author: User

To be clear, Java is passed by value! In the Java world, it is all passed by value. In fact, by value, by reference are just a concept, the key is how we understand the nature of Java parameters. Here's a case in point where, in some cases, the value passed is a reference, which can easily be confusing.
Take a look at the following example: First, the following code
Dog Mydog = new Dog ("Rover"); foo (mydog);
At this point, you pass the Foo function a value, the content of which is rover the memory address of this dog (this is only a rough explanation, because in Java, this address is not the real address) assuming this time rover the dog's address is 42, So we're just passing 42 this address to the Foo method.
public void foo (Dog somedog) {    somedog.setname ("Max");     AAA    Somedog = new Dog ("Fifi");  BBB    somedog.setname ("Rowlf");   CCC}
In the Foo method, is a procedure 1, the value of the parameter Somedog is an address (42) 2, in the AAA line: Because Somedog represents 42 that dog, 42 the name of the dog, from Rover was changed to Max3, in the BBB this line: This is the key place, Another new dog,new means that a dog is placed in the new address space, and we assume that the address is 74. At this point, the value of Somedog will be changed from 42 to 744, in the CCC line: This time the modification, is 74 that dog's modification, not 42 Therefore, for Foo's parameter somedog, it is a value, not a reference. It is a value, so his value changes arbitrarily, for example from address 42 in the example to 74. It is not a reference. If it is a reference, then the changes within the Foo method (including the BBB, CCC, or both) should have an effect on the dog of the 42 address space.
not scrutiny concept, and then summarize the Java beginner prone to make mistakes in the place:If you think of an example,"Object A = new object ();foo (a);//Modify the property value of a within the methodreturn a;//Hope A is already a modified value at this point"
It is important to remember that this objct must be initialized before invoking the method (new), then passed in as a parameter, and then modifying some of the Bean's properties in the method before it affects the object previously initialized, and if the new operation is performed within the method, It's actually pointing to the other address space.
Knowledge Supplement: 1, what is value delivery? Refers to when a method call passes a parameter that is passed by a copy of the value. Pass important characteristics by value: Pass a copy of the value, that is, the transfer is not related, each value corresponds to an address space 2, what is a reference pass refers to the method call, the passed parameter is passed by reference, in fact, the address of the reference of the variable is the corresponding memory space. A reference to a value is passed, meaning that both before and after delivery point to the same reference (that is, the same memory space).
StackOverflow Link Http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value


Column Introduction: very like StackOverflow, can always find a solution to the problem of incurable diseases. Accidentally found that the site has a list of heat. So select some of the more hot questions, read the answers to each question, and then sort them out according to your own understanding. Therefore, these articles are not true translation, but in accordance with their own understanding of a number of additions and deletions, polishing, hoping to put the above discussion, more streamlined and effective sharing to everyone. if you want to reprint, please specify the original address Http://blog.csdn.net/lizeyang

"StackOverflow Good question" Java parameters are passed by value or by reference

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.