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

Show parent comments

1

u/Jarl-67 May 18 '20

It isn’t pass by value because you say so. Logic says otherwise. The parameter is an object, and it’s the entire object that needs to be by value. The only reason you think it’s by value is that that the actual reference (key word) is not changeable. When a parameter is by value, no part of the parameter is changeable.

Didn’t you learn this in college? I assume you have a degree but maybe that’s too big of an assumption.

This isn’t a belief by the way, you are the one who believes. That’s why you can’t actually argue the point but instead just restate your untenable position.

1

u/wildjokers May 18 '20

I can now see you are just trolling.

0

u/Jarl-67 May 18 '20

In your world view, references are not references. Got it!

1

u/random741f May 20 '20 edited May 20 '20

It isn’t pass by value because you say so. Logic says otherwise. The parameter is an object, and it’s the entire object that needs to be by value. The only reason you think it’s by value is that that the actual reference (key word) is not changeable. When a parameter is by value, no part of the parameter is changeable.

The parameter is a memory address. It's the memory address that needs to be by value. Yes no part of the parameter is changeable. You can't change the memory address. :D

https://www.educative.io/edpresso/pass-by-value-vs-pass-by-reference

Pass by value means that a copy of the actual parameter’s value is made in memory

Pass by reference (also called pass by address) means to pass the reference of an argument in the calling function to the corresponding formal parameter of the called function so that a copy of the address of the actual parameter is made in memory, i.e. the caller and the callee use the same variable for the parameter. If the callee modifies the parameter variable, the effect is visible to the caller’s variable.

Clearly every single person on StackOverflow is wrong and you are just right. :)

https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value
https://stackoverflow.com/questions/7893492/is-java-really-passing-objects-by-value
https://stackoverflow.com/questions/9109529/pass-by-value-and-pass-by-reference-in-java
https://stackoverflow.com/questions/12757841/are-arrays-passed-by-value-or-passed-by-reference-in-java
https://stackoverflow.com/questions/24101960/why-is-java-pass-by-value-only

Go ahead and argue with everyone on StackOverflow. Yep you're an amateur not even working in the industry. :)

You know what they say, "If everyone you meet is an asshole, you're the problem."

I do not actually believe you are trolling. I do think you have very different definitions of pass by reference than everyone else in the world.