Note: There is no reference transfer in Java

Source: Internet
Author: User

Note: This article applies to Java beginners. If any reader finds that there is something wrong with the article, please correct it.

On the Forum today, someone raised a question about the absence of reference transfer when calling functions in Java. Some people say that Java only supports value passing, and some people say that Java has both value passing and reference passing. If there is any reference passing in Java, let me analyze it.

I. First, let's clarify the differences between "value transfer" and "reference transfer ".

Value Transfer: a copy of the passed parameters is performed. Modifications to the parameters are only modifications to the replicas. The function call ends and the replicas are discarded. The original variables remain unchanged (that is, the real parameters remain unchanged)
Reference transfer: When a parameter is passed to a function, the parameter is passed directly to the function without copying a copy. Any changes to the parameter in the function are reflected in the original variable.
Ii. Definitions referenced in Java

Both C ++ and Java have referenced concepts, but they have completely different meanings in these two languages. In C ++, we can define a reference B of variable A in the form of "Int & B = A". B is just an alias of, B and A occupy the same storage unit in the memory. Using the reference mechanism, we can implement two-way transfer of the Present value when calling a function-that is, reference transfer. See the following code:

Example 1

# Include <iostream>

Using namespace STD;

Int main ()

{

Void swap (Int &, Int &);

Int I = 3, j = 5;

Swap (I, j );

Cout <"I =" <I <"J =" <j <Endl;

Return 0;

}

 

Void swap (Int & A, Int & B)

{

Int temp;

Temp =;

A = B;

B = temp;

}

The above program outputs I = 5 J = 3. When a and B are passed to the swap () function, their own addresses are passed, not their copies, so their changes in the function can directly affect the real parameters A and B, which is the reference transfer.

References in Java are more like pointers in C ++. When we define an object (such as person P = new person (), the defined object instances will be placed in the Java heap, the Variable P (reference) is placed in the java stack, and P points to the person object instance in the heap.

Iii. Misunderstanding of reference Transmission

Why do many people think that Java has a reference transfer? One case is that some people think that when calling a function, its parameter may be referenced (above P), so Java has a reference transfer, and this part of the person does not have a correct understanding of the reference transfer; the other situation seems reasonable, but careful analysis is also incorrect. They often use the following code to prove their points:

Example 2:

 

View plaincopy to clipboardprint?
  1. Class demo {
  2. Int;
  3. Public demo (int ){
  4. This. A =;
  5. }
  6. }
  7. Public class testquote {
  8. Public static void main (string ARGs []) {
  9. Demo d1 = new demo (1 );
  10. Demo D2 = new demo (2 );
  11. System. Out. println (d1.a );
  12. System. Out. println (d2.a );
  13. Function (D1, D2 );
  14. System. Out. println (d1.a );
  15. System. Out. println (d2.a );
  16. }
  17. Private Static void function (Demo D1, demo D2 ){
  18. Int;
  19. A = d1.a;
  20. D1.a = d2.a;
  21. D2.a =;
  22. }
  23. }

 

Their point of view is as follows: execute the above Code, and the result output before calling function () is 1 and 2. After calling function (), the output result will be 2 and 1, it can be seen that the changes to D1 and D2 in the function reflect the original variables. If it is not, it will not output 2 or 1.

This explanation is confusing and seems to be correct. The code below will refute the above point of view:

Example 3:

 

View plaincopy to clipboardprint?
  1. Class demo {
  2. Int;
  3. Public demo (int ){
  4. This. A =;
  5. }
  6. }
  7. Public class testquote {
  8. Public static void main (string ARGs []) {
  9. Demo d1 = new demo (1 );
  10. Demo D2 = new demo (2 );
  11. System. Out. println (d1.a );
  12. System. Out. println (d2.a );
  13. Function (D1, D2 );
  14. System. Out. println (d1.a );
  15. System. Out. println (d2.a );
  16. }
  17. Private Static void function (Demo D1, demo D2 ){
  18. Demo temp;
  19. Temp = D1;
  20. D1 = d2;
  21. D2 = temp;
  22. }
  23. }

 

After executing the code above, the program outputs 1 and 2 before and after calling function (). This program tries to exchange D1 and D2 by calling function (), but it fails. Why? Because D1 and D2 are value passing, D1 and D2 in function () are copies of D1 and D2 in main () function. After function () is called () variables in. In the second example, the function () function does not change the values of D1 and D2, but the values of the objects indicated by D1 and D2 () afterwards, D1 and D2 still point to the heap address before the function call, that is, the function parameters are D1 and D2 in the stack, rather than the objects directed by D1 and D2 In the heap, even if you change the object in the heap in the function, the value of the function parameter is not changed. So Example 2 is not a reference transfer. It can be seen that only values are transmitted in Java.

 

However, there are many articles on the Internet regarding the difference between "Java value passing and reference passing". If you see it, you must be clear. The reference passing mentioned in this article is incorrect, the reference transfer they mentioned is the case in example 2. Unfortunately, many articles on the Internet regard the transfer in example 2 as a reference transfer. If the reader sees what it means, it will be fine.

 

From http://blog.csdn.net/lubiaopan/archive/2009/11/19/4837878.aspx

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.