Please note: There is no reference passing in Java

Source: Internet
Author: User

Description: This article is intended for Java beginners, if there are readers found that the article is inappropriate, please correct me.

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.

First, to clarify the difference between "value passing" and "reference passing"

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)
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.
Second, the meaning of the reference in Java

There 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:

Example One

#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;

A=b;

B=temp;

}

Execution of the above program output is i=5 J=3,a and b passed to the Swap () function, is passed their own address, not their copy, so in the function of their changes can directly affect the arguments A and B, which is the reference pass.

References in Java are more like pointers in C + +, when we define an object (such as person p=new person ()), 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.

Three, the misunderstanding of the reference transmission

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:

classdemo{intA;  PublicDemo (inta) {            This. a=A; }   }    Public classtestquote{ Public Static voidMain (String args[]) {Demo D1=NewDemo (1); Demo D2=NewDemo (2);           System.out.println (d1.a);           System.out.println (d2.a);           function (D1,D2);           System.out.println (d1.a);       System.out.println (d2.a); }       Private Static voidfunction (Demo D1,demo D2) {intA; A=d1.a; d1.a=d2.a; d2.a=A; }   }  

Their 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 will be 2, 1, visible in the function of the D1 and D2 changes reflected on the original variable, if not output 2, 1.

This explanation is confusing and seems to be correct, and the following code will be a good rebuttal to the point above:

Example three:

classdemo{intA;  PublicDemo (inta) {            This. a=A; }   }    Public classtestquote{ Public Static voidMain (String args[]) {Demo D1=NewDemo (1); Demo D2=NewDemo (2);           System.out.println (d1.a);           System.out.println (d2.a);           function (D1,D2);           System.out.println (d1.a);       System.out.println (d2.a); }       Private Static voidFunction (Demo D1,demo D2) {Demo temp; Temp=D1; D1=D2; D2=temp; }   } 

Execute the above code, call function () before and after the program output is 1, 2, this program tried to call function () to Exchange D1 and D2, but did not succeed, 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.

But 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.

Please note: There is no reference passing in Java

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.