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/FurkinLurkin May 15 '20 edited May 15 '20

Removed so I don't muck up the real answer

5

u/GuyWithLag May 15 '20

Java does not have pass-by-reference.

Given a local variable declaration Foo x = ...;, there is no way for a method that takes x as a parameter to change the value of x outside its scope.

Java is strictly pass-by-value; for object references the reference to the object is passed as a value.

3

u/FurkinLurkin May 15 '20

^ probably wording it better