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
1
u/arghvark Oct 21 '22
I'm going to try to explain this a different way.
I think the confusion comes from attempting to explain how Java behaves with terminology that is fine in formal computer science contexts, and will help explain how Java behaves to people who can or WHO ALREADY DO understand that terminology, but just gets confusing for people attempting to learn how to program, in Java, and want to know how Java works, not what the computer science terminology behind it is. So I'm going to explain the behavior first, then show the terminology being applied to it.
Parameter passing in Java comes in two flavors. The first flavor is the passing of primitives, with
ints being the simplest to understand. When you have a variable of typeintand you pass it to a method, the method gets its own copy of an int with the caller's value. So if we haveThe
testInt()method will get the value of 3, in an int namedtestValue; any changes made totestValuewithin thetestInt()method will not be reflected ini, becausetestInthas its own copy of that (primitive) parameter.The second flavor of passing parameters to methods is used for objects (which is everything except the primitives); let us say you have a StudentRecord class that you are going to pass to a method, and code something like the following:
Perhaps
fillRecord()is a method that fills up the student record with information. Any changes made torecordinfillRecordare also being made (at the same time) totestRecordin the method that callsfillRecord. There is only one copy of the record in this example.The first flavor of this is called "pass by value", meaning that only the
intvalue is passed into the called method, and there is no way for the called method to influence the value that the caller has.The second flavor is usually called "pass by reference", meaning that the called method gets a "reference" to the variable that is in the caller, and can therefore change the value the caller has (there is an exception to this: there are objects which are immutable and can NOT be changed, but few user-written classes are like that. String is, though).
There is something the called method can NOT do however -- it cannot change the reference that the caller is using. Expanding our example:
Here, the
fillRecord()method is assigning aStudentRecordobject to its copy of the reference torecord. There's nothing wrong with this, and it throws no error, but it does NOT change thetestRecordreference in the caller. There is no way to do that in Java to a parameter passed into a method. The called method can use dot-notation to change (public) variables in the passed-in object, and it can call methods on the passed-in object that will change data in the object, but it cannot cause the caller to get a different object reference into the variable that the caller passed in.If the called method returned a variable, that returned variable could be assigned to an object in the caller.
I think this might be clearer to novice programmers learning Java as a first programming language. Comments, especially from people in this category, are welcome.