Java – why do programmers say “pass by reference” is actually “pass by value?” Why is this important?

I know in C & amp; The entire concept passed by reference in C. And similar concepts only pass values in Java

void swap(int &x,int &y)

But passing through the pointer reference in C just passes the pointer by value

void swap(int* x,int* y)

I know this question may seem silly, but I think there is a huge loophole in the whole concept So what is the actual definition of a call by reference, which is just the pseduo name of the call by value in another context?

Solution

Two key points:

>There is no reference in C. > Passing by value and by reference is different They are different

Pass by value: the called function creates a new set of variables on the stack and copies the parameter values into it

Pass by reference: instead of passing the value to the called function, you pass a reference / pointer to the original variable

When passing a reference / pointer to the original variable, the object / address is actually passed by value Therefore, you can say that passing by reference is passing the reference by value, but this does not mean that passing by reference is passing the pseudo name of the value The difference between the two is well explained in this answer I'm copying excerpts:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>