r/javahelp • • May 15 '20

how reference variables are passed as method arguments in Java

Hello, I am struggling to understand what this question is asking for, I know in C++ for pass by reference the address of the variable is passed and altered before returning unlike pass-by value(correct me if Im wrong) but in java I thought all values were automatically pass by reference? struggling to make sense of the question so any help at all will be greatly appreciated:)

Explain the difference between how pass-by-reference operates in C++ and how reference variables are passed as method arguments in Java. A good answer will include a simple example of each

16 Upvotes

30 comments sorted by

View all comments

-1

u/Jarl-67 May 17 '20

There are a lot of definitive "Java is pass by value only" statements in the comments. This disproves that false narrative. Examples given include primitives and the reference itself but that is only partially pass by value. Java passes the pointer by value and that's it. The object is modifiable as the reference to that object is the same. No copy of an object parameter is made in java.

Java pass by reference example

For parameters that are pass by value, it's impossible to change a parameter where that change lasts past the method execution. In this simple example, you can see that an object is modified and that modification persists after the method is complete. This situation is impossible in a pass by value scenario.

Running the example yields this result:

Before method: Joe "by Value" Java

Start method: Joe "by Value" Java

End method: Joe "by Reference" Java

After method: Joe "by Reference" Java

1

u/random740f May 17 '20

In case anyone stumbles upon this thread in the future:

That is still pass by value... Like I said go back and study your Java fundamentals.

I never said you could not modify the internal fields within the object itself. I said modify the actual memory address. What you are saying is you are an amateur (I actually work in the industry as a professional) who does not understand what "pass by reference" actually means.

Show me how to modify the memory address of a variable in the caller from the called method itself:
https://repl.it/repls/CarefulHideousBusinesssoftware

As I said, you are 100% wrong because that C++ code is impossible in Java.

Clearly everyone in this StackOverflow thread is wrong and you're right:
https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value/40523#40523

https://www.reddit.com/r/javahelp/comments/9gkzt6/why_does_java_pass_parameters_by_value_instead_of/

What would you need pass by reference for? Java doesn’t have C/C++ pointers. Object references are passed by value, which still allows you to update the internals of an object.

Did you even read the OP?

Explain the difference between how pass-by-reference operates in C++ and how reference variables are passed as method arguments in Java.

That question would not make sense if pass-by-reference existed in Java.


This disproves that false narrative. Examples given include primitives and the reference itself but that is only partially pass by value.

Nope. Basically you are telling me that you are an amateur and you have no idea what pass by reference actually means.

As I said, I think you are one of those people that can not just admit when they are wrong. :)

1

u/Jarl-67 May 17 '20

"Nope." because you said so? That's your argument? Like I said, it's impossible to modify a Pass by value parameter and that is true in any language. Cleary you are able to do that in java. References and pointers are the same thing. Only the reference/pointer in java is pass by value.