Note that there is no reference in Java to Pass-----reprint

Source: Internet
Author: User

1 Description: This article is intended for Java beginners, if there are readers found that the article is inappropriate, please correct Me. 2 3 On the Forum today, Someone has raised a question about the invocation of a function in java, which is a noisy thing. Some people say that Java is only a value to pass, and some people say that Java has both value passing and reference passing, then there is no reference in Java to pass it, let me analyze it. 4 5first, to clarify the "value passing" and "reference passing"difference6 7 value passing: is a copy of the passed parameters, the modification of the parameters is only the modification of the copy, the end of the function call, the copy is discarded, the original variable (that is, The argument is not changed)8 reference passing: When a parameter is passed to a function, it is not copied, but the parameter itself is passed directly to the function, and any changes to the parameters within the function are reflected on the original Variable. 9 second, The meaning of the reference in JavaTen  oneThere are references in both C + + and java, but there are completely different meanings in both Languages. In C + + we can define a reference to variable a in the form of "int &b=a", B,b is just an alias of a, B and a in memory the same storage unit, using the reference mechanism we can implement the value of two-way transfer when calling the Function-that is, reference passing, see the following code: a  - Example One -  the#include <iostream> -  - using namespace std; -  + intMain () -  + { a  at   voidSwapint&,int&); -  -   intI=3,j=5; -  - Swap (i,j); -  incout<< "i=" <<i<< "j=" <<j<<endl; -  to   return0; +  - } the  *   $ Panax Notoginseng voidSwapint&a,int&B) -  the { +  a   inttemp; the  +temp=a; -  $A=b; $  -b=temp; -  the } - WuyiExecution of the above program output is i=5 j=3, A and B are passed to the swap () function, they are passed their own address, not their copy, so the changes in the function can directly affect the arguments A and b, which is the reference pass.  the  -References in Java are more like pointers in C + + when we define an object, such as person p=Newperson ()), The defined object instance is placed in the Java heap, and the variable p (that is, the Reference) is placed in the Java stack, and P points to the person object instance in the Heap.  wu  - three, The misunderstanding of the reference transmission about  $ Why does a lot of people think that Java has a reference pass? One case is that some people think that the invocation of a function may be a reference (such as P above), so Java has a reference to pass, this part of the reference pass is not the correct understanding, and the other seems reasonable, but careful analysis is not correct, they tend to use the following code to prove their point of view:  -  - Example Two: -  a Copy Code + classdemo{ the      inta;  -       publicDemo (intA) {   $           this. a=a;  the      }   the  }   the   public classtestquote{ the       public Static voidmain (String Args[]) { -Demo d1=NewDemo (1);  inDemo d2=NewDemo (2);  the System.out.println (d1.a);  the System.out.println (d2.a);  about function (d1,d2);  the System.out.println (d1.a);  the System.out.println (d2.a);  the      }   +      Private Static voidfunction (Demo D1,demo d2) { -          inta;  theA=d1.a; BayiD1.a=d2.a;  theD2.a=a;  the      }   -  }   - Copy Code theTheir views are as follows: execute the above code, call function () The result of the previous output is 1, 2, Call function () function after the output of the result will be 2, 1, it can be seen that the changes to D1 and D2 in the function are reflected on the original variable, but not 2 or 1.  the  the This explanation is confusing and seems to be correct, and the following code will be a good rebuttal to the point above: the  - Example Three: the  the Copy Code the classdemo{94      inta;  the       publicDemo (intA) {   the           this. a=a;  the      }  98  }   about   public classtestquote{ -       public Static voidmain (String Args[]) {101Demo d1=NewDemo (1); 102Demo d2=NewDemo (2); 103 System.out.println (d1.a); 104 System.out.println (d2.a);  the function (d1,d2); 106 System.out.println (d1.a); 107 System.out.println (d2.a); 108      }  109      Private Static voidfunction (Demo D1,demo d2) { the Demo temp; 111temp=d1;  thed1=d2; 113D2=temp;  the      }   the  }  the Copy Code117Execute the above code, call function () before and after the program output is 1, 2, this program attempts to exchange D1 and D2 by calling function (), but not successfully, why? Because D1 and D2 are value passes, d1 and D2 in function () are copies of D1 and D2 in the main () function, and the function () does not affect the variables in main (). In the example two, the function () functions change not the values of the D1 and D2 itself, but the values of the objects that D1 and D2 point to, and D1 and D2 still point to the heap address before the function call when the function () is called, which is the D1 and D2 in the Stack. Instead of the objects that the D1 and D2 point to in the heap, even if you change the objects in the heap in the function, the values of the function arguments are not changed. So example two is not a reference pass; only values are passed in Java. 118 119   - 121But there are a lot of articles on the web for "the difference between Java value passing and reference passing", and if the reader sees it, it must be clear that the reference passing is incorrect, and that the reference passing is the case in example Two. unfortunately, There are many articles on the web that refer to the passing of the example two as a reference, if the reader sees what it means.

Note that there is no reference in Java to Pass-----reprint

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.