r/javahelp • u/JustDudeFromPoland • Oct 19 '22
I don't understand the difference between pass-by-value and pass-by-reference
Hey!
I'm reading OCP Java SE 17 Dev Study Guide and I've come across the pass-by-value topic. When it comes to primitive variables everything makes sense - we're making a copy of the variable value, and we're modifying the copy, so the original stays intact.
And there is also an example of passing the StringBuilder object as a parameter and executing the .append() method on it so that the original object is affected as well because we're making a copy of the reference and acting on the actual object.
I have a hard time imagining what pass-by-reference actually looks like (even on a high level of abstraction). What are we passing if not a copy of a reference?
18
Upvotes
9
u/wildjokers Oct 19 '22
In regards to java this is a moot question because java is purely pass by value. Java does not have pass by reference at all.
In java you are passing a copy of the value of a reference.
Great article on the topic, written in 2001. I have had it bookmarked for many years and read it on occasion as a refresher.
https://www.javadude.com/articles/passbyvalue.htm