Java Pass by Value or Pass by Reference

This post is about one of the basic question whether Java is pass by value or pass by reference. Though basic, this question causes some confusion mainly for objects passed as parameter. In this post let’s go through some examples to clarify the doubts and have a clear understanding of the topic.

To start with first have an understanding of the two terms used here pass by value and pass by reference.

  • Pass by value– In pass by value the value assigned to the variable is copied to another variable. That another variable uses the copied value with in its scope without modifying the original value.
  • Pass by reference– In pass by reference the alias to an original variable is passed to the method parameter.

Java is pass by value

In Java whatever type of data you pass whether primitive or an object, it is always pass by value. It is easy to understand for primitive values but a little confusing when it comes to objects. The confusion originates because of the terminology used, when object is passed as a parameters what we say is object reference is passed.

It’s high time to see some examples to get a clear picture.

Passing primitive as method parameter


Here you can see that an integer variable is passed as a parameter to the method, in the method value of the passed parameter is changed but that doesn’t affect the original variable. That shows the actual variable’s value is copied to the parameter so it is pass by value.

Passing object as method parameter

Now comes the object part which needs some explanation. When you pass an object it is true that the reference to the memory allocated for that object is passed but there is a subtle difference when that reference is passed as value as compared to when it is passed as reference.

When object reference is passed as value the memory location stored in the variable is copied to the method parameter. Which means changing the object fields in the method will be reflected everywhere but changing the reference won’t change the reference for the original object.

But in case of pass by reference an alias is passed which means even the reference can be changed.

Example code showing object fields can be changed


In the above code you can see that the object is passed to the displayValue() method and there the fields num is modified. The output shows that the field is changed in the calling method too.

This statement PassValue pv = new PassValue(7); creates a new object of class PassValue and initializes it to have value 7 for variable num. Here pv is the variable that stores the reference to that created object.

obj reference in Java
When you call method in this statement – pv.displayValue(pv); the object reference stored in pv is copied to the obj parameter. Both pv and obj share the same reference.

shared reference

As shown in the image both variables pv and obj share the same memory reference so the change made to the field num using object obj is visible when num field is retrieved using pv object.

Example code showing object reference can’t be changed


Here you can see that a new object is created and assigned to the obj parameter in the displayValue() method. But that doesn’t change the pv object reference which shows even for objects Java is pass by value.

Java pass by value

That’s all for the topic Java Pass by Value or Pass by Reference. If something is missing or you have something to share about the topic please write a comment.

You may also like

One Comment

  1. Pingback: Object Class in Java - KnpCode

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.