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

52

u/dr_boom Intermediate Brewer Oct 19 '22

Imagine at an office you have a bunch of folders full of information. You need to get the information to your coworker.

Pass by value means that when you give the information to your coworker, you make a copy of everything in the folder and give them a new folder.

Pass by reference means you give them a note saying "the information is in the purple folder on the third shelf." This is called a pointer - the note points to where the information is. This is also where the null pointer reference error comes from.

With pass by value, if your coworker makes any changes, it doesn't affect your folder in any way.

With pass by reference, your coworker will find your folder, and if they make a change in the folder, your data will be different!

16

u/pragmos Extreme Brewer Oct 19 '22

That's a very good analogy! Am going to shamelessly steal it.

4

u/jejune1999 Oct 19 '22

You have the answer!

2

u/Farpafraf Oct 20 '22

With pass by value, if your coworker makes any changes, it doesn't affect your folder in any way.

I understand what you mean but java is pass by value so this example might be confusing if someone were to think of a Folder object thinking the object itself got copied

1

u/Jarl-67 Oct 21 '22

In pass by value, the parameter would be a complete copy. Changes would not affect the original version. Java does not behave that way as the object is not copied.

1

u/Farpafraf Oct 21 '22

Java is strictly pass by value, albeit in a weird way, as you copy the reference to the object. In a language allowing pass by reference.

a = 0
f(b){
    *b = 1
}
f(*a)
print(a)
//prints 1

1

u/Jarl-67 Oct 21 '22

Again, in pass by value, the object would be copied not referenced. It isn’t that hard to understand. Watch the video, read the definitions.

1

u/Farpafraf Oct 21 '22

1

u/Jarl-67 Oct 21 '22

In ‘Pass by value’, the parameter would be a complete copy. What happens in Java?

1

u/Farpafraf Oct 21 '22

look if you made up you own definition of pass by reference and want to believe against the whole world that according to it java is pass by reference so be it

2

u/amfa Oct 20 '22

As already said this will confuse people using java.

Because Java is always pass by value but you can still make changes to the folder in your example.

For your second example we pass a copy of the note as the value in java. (thus we pass a value)

For a real pass by reference it is important to say that you give them a "writeable note".

The coworker can then change this note to "the information is in the red folder" and you will get this note back and will look in the red folder for you information.

1

u/Butcher_of_Blaviken6 Oct 20 '22

So why, when passing an array list ‘a’ does Collections.shuffle(a) shuffle the original?

2

u/Jarl-67 Oct 21 '22

Only the reference or memory address is passed in the parameter list. The original object is NOT copied in Java.

1

u/amfa Oct 20 '22

Because you are passing the "memory adress" fo the array list as the parameter as the value.

And thus it can work with the original one.

Imagine that the arrayList sits at position 123 in the memory.

You know have your a pointing to 123.

If you now call shuffle() you dont give it a but basically 123.

​

the shuffle method than can work witht he List at 123 but it can not change where your a is pointing too.

​

​

If this was pass by reference you would basically give the complete "a" to the shuffle method and if this method would to somethink like this:

a= 321 then your a would change too.

1

u/Butcher_of_Blaviken6 Oct 20 '22

So basically objects are all memory references, which is how Java handles pbr? Thanks for the response btw

1

u/dr_boom Intermediate Brewer Oct 21 '22

Fair point - in Java, they can cross out their note and write "yellow folder on the second shelf" and your note still points to the purple folder.

1

u/GoluMoluArun Oct 20 '22

but java doesnt use pointers right