Java — pass by reference or pass by value?

Nemanja Žunić
Java Vault
Published in
4 min readApr 28, 2018

You might have heard of terms ‘pass by reference’ or ‘pass by value’ before but never really gave it much thought. You might have tried to pass a variable to the function and tried to change its value but failed to do so and never really wondered why.

In this post, I’ll explain another one of the inner workings of Java language that every developer should have.

Pass by Value

When we say that in Java we “Pass by Value” we mean to say that when argument is passed from the caller function to the callee function it is copied and stored into a brand new variable. Variables that the caller function passes and variables that callee function gets as argument behave totally independent one from another. Let me display what I mean by an example:

We can see here on line 10 that the value of variable numb = 15;. Here is the current state of the Stack (if you are not familiar with the Stack and Heap I recommend that you read my previous post):

When we pass numb to the callee function on line 11: callee(numb); , value of variable numb is copied into variable arg. So arg will initially have value 15, same as numb:

Those two variables do have the same value for now, but are independent. So when we change value of arg = 1; on line 16, value of numb in the caller function remains unchanged:

To prove this concept, on line 17 our application prints “Value in CALLEE is: 1”, but on line 12 our application prints “Value in CALLER is: 15”.

When working on Java apps please bare in mind that Java is “pass by value” and that variables cannot be changed when passed to the other function.

Passing objects in Java

But what happens when we pass an object? Surely getting an independent copy of an object in a function, without the ability to change the original is of no much use to us.

Well, in the previous post I explained that variable can point to the object in the Heap. Variable’s value is not an object itself, but rather a pointer to the object stored in the Heap.

So, when we pass an object to the function, what gets copied in a new variable is not an object itself but a pointer to the object in the Heap.

What this means is that we copied pointer to the original object in the Heap and now we can use this pointer to access the object, read or change its state.

Take look at this example:

Here we can see on line 10 that person’s height is initially 185:

When callee function gets called on line 12: callee(person); new variable p is created and it’s value becomes pointer that person variable holds:

Pointer is copied from variable person to variable p, same as with integers in the previous example. But now we can use the pointer to access the object it points to. On line 17 in App.java callee function manages to change person’s height to 200:

And because person variable in caller function points to the same object on the Heap, this new state of the object can be accessed on line 13 and displayed in the console.

Important thing to remember here is that Java copies pointers when calling function with object parameters. And by using those pointers we can change the object’s state. This change will be visible anywhere in the program.

Conclusion

This is one of the basic concepts in Java that must be understood by all the developers. Hope I gave you a solid explanation 😄 If you have any problems with understanding the concepts explained or suggestions for an improvement feel free to contact me. Happy coding everybody, cheers 🍹

--

--

Nemanja Žunić
Java Vault

I write sentences that make the magic happen (software developer, basically the same thing).