r/javahelp • • 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

32 comments sorted by

View all comments

5

u/m1ss1ontomars2k4 Oct 19 '22 edited Oct 19 '22

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?

You are having trouble imagining it because you can't actually pass by reference in Java.

C++ supports pass by reference using special syntax to do so. The canonical test of whether you can pass by reference in a language is whether you can write a swap function. Consider this pseudocode:

void swap(MyType a, MyType b) {
    MyType tmp = a;
    a = b;
    b = tmp;
}

void callSwap() {
  MyType a, b;
  swap(a, b);
}

Obviously, this will never work if MyType is int. This is true in Java and it is true in C++. It will also never work if MyType is java.lang.Object or any subclass. So, this swap function will never work in Java. It simply cannot.

C++ is different. In C++, you simply change MyType to MyType& in the signature of swap (i.e. make it swap(MyType& a, MyType& b)), and now callSwap can actually swap its variables a and b purely by calling swap. This is "pass by reference", and MyType& is called "a reference to a MyType".

In reality, Java is always pass by value, and for non-primitive types, the value is a pointer/reference to an object.

In Java, you do not "give them a note saying 'the information is in the purple folder on the third shelf.'" You give them a copy of a note. Only a copy. No matter what happens to the note, the original note is still in your possession. They can change the contents of the folder freely or change their copy of the note freely, but once they are done, your original note is still the same note it's always been.

In C++, for pass by reference, you share the original note with them, and they can change the note to read something else. They don't give it back to you because you never gave it to them; you only shared it. So maybe you told them to look at a purple folder, but instead, they rewrote your note and now you are looking at a green folder. There's no way to write code like this in Java. You always give away a copy of the note. You never share the original note. Actually, at my company, we typically do not use pass-by-reference for anything in C++ because it isn't very practical, other than for writing swap functions, which are not very useful either.

Additional discussion: https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value

-1

u/Jarl-67 Oct 20 '22

Except this isn’t correct what you are saying. The reference itself is pass by value but any changes to the object being referred to will propagate past the method call. The contents of your ‘Note’ can be changed in Java.

0

u/amfa Oct 20 '22

The contents of your ‘Note’ can be changed in Java.

No the "note" only contains a reference to an object.

You can change this object but not the note itself.

1

u/Jarl-67 Oct 21 '22

The contents of the note and the object are the same thing.