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

1

u/djpfg May 15 '20

so say you pass a int x=4 into a square(int x) method, the value of x before being passed is stored in file location 0xFF04, when the value returns as 16 the pointer to x will move to file location 0xFF08 and 16 will be stored there and that is what will show in the code?

Thank you very much for your response, I have my final exam tomorrow and this question freaked me out

3

u/FurkinLurkin May 15 '20

Sorry for confusing the subject. After reading that javadude.com linked article in someone else's answer I realized I had the definitions of what a reference and what a value is swapped. The article is a pretty good explanation and has the proper examples that I was fumbling to give.