0

So this is a question about jvm design, since basically java is pass by value, and what is actually pass into method is object's reference address copy, then why we do that instead of passing the origin reference address? would this cause any trouble in practice? Thanks.

SveinFz
  • 7
  • 2
  • https://stackoverflow.com/a/373429/1554844 They're two different things with two different semantics. What else do you want to know? – Vaughan Hilts Jun 06 '19 at 06:24
  • 1
    Not sure I understand the question. Are you asking why `foo(x)` is not given the address of the object that `x` points to? Simple answer: That is exactly what it is given. Or are you asking why `foo(x)` is not given the address to the variable `x` itself? That would be pass-by-reference (which Java does not do, also inefficient if you just want to get to the object pointed at by `x` -- why the extra indirection). – Thilo Jun 06 '19 at 06:57
  • What do you mean with “origin reference address”? – Holger Jun 06 '19 at 07:02
  • "Reference address" is just a number. "Copy of a number" does not make sense. What's the difference between 1234 and "copy of 1234"? This is just the same set of bits. – apangin Jun 06 '19 at 07:07

1 Answers1

1

First, be very clear that in JAVA everything is PASS BY VALUE(also called PASS BY COPY). Secondly, hope you are very clear that passing 'References' in JAVA doesn't mean passing some kind of pointer(like in C), it simply means passing the COPY of 'reference variable' storing the address of the actual object living on the heap.

The copy of address is passed rather than passing the original one since in JAVA everything is structured as pass by copy because of some reasons such as Simplicity of the language, Encapsulation(Security), etc. and everywhere in JAVA its followed.

Passing by copying makes the language simpler, you don't have to think about something like pointers or anything while making the signature of the function which makes the language even more adorable.

Also, Pass by copy aids in encapsulation making the language more secure, as this step makes methods disabled to modify variables outside their own scope.

One more thing is referencing(passing by reference) may impose more constraints and rules in the language.

--Thanks for Asking.