Is Java Call By Value or Call By Reference?

Most of the people who come from C or C++ background get a lot of confusion whether Java is call by value or call by the Reference language.

In this post we are going to check if Java is call by value or reference?

If you’re looking for a one line answer, here it is :

Java is call by value

What we will do in this post is we will take three scenarios and will try to make sure that you understand why I’m calling it a call by value.

Scenario 1 – Call with Primitive

The first scenario would be when you are calling a method using primitive values.

So here is a small program where I am calling a method with a single parameter. Parameter value is integer and I am trying to change the value of the parameter inside the function.

https://gist.github.com/SanjeevMohindra/7189c03fe55e1accf2d8

Here is the output of the above program:

Value of x before the call: 10

Value of x after the call: 10

You can see that the value has not changed for the main function. It means that the parameter was pass to add function by value. A new local variable has been created on stack and the value has been copied to the new variable.

Java Call With Primitive

The change of the value has happened to new variable and its not visible to main function.

Scenario 2 – Call with Object

The second scenario would be when you are calling a method using objects. Lets create a object which can contain an integer property and allow us to read and change that value.

We would be using the similar program as above but rather than passing a primitive, we will pass this new object.

https://gist.github.com/SanjeevMohindra/06e382c1670e23fa5dea

This program generates the below output:

Value of x before the call: TempNum [x=10]

Value of x after the call: TempNum [x=15]

This is one of the confusing example. We said Java is call by value but the change in add function is visible to main. So it should be by reference.

It is still call by value, what is happening is a new reference variable is getting created and object reference is copied to this new variable. Now we will have two reference variable pointing to same object.

Java Call With Object

So both main and add function hold a reference variable, which is pointing to same object. So any changes made to object from main or add are visible to each other, because they are happening to same object in heap.

To verify this theory, we have our third scenario.

Scenario 3 – Call with Object With Create

The third scenario would be when you are calling a method using objects and creating new objects inside the called method.

We are using the similar code as scenario 2, but rather then changing the passed object directly, we are creating a new object inside the add method.

https://gist.github.com/SanjeevMohindra/63341c3c5ac02e9423dc

If we run this program, we will get the below output:

Value of x before the call: TempNum [x=10]

Value of x after the call: TempNum [x=10]

Now, the change of the value inside add function is not visible to main function. This is because there are two separate reference variables and their visibility is limited to their functions.

Java Call With Create Object

The newly created object is not visible to main function and it can’t access that variable.

So finally we can say that Java always pass parameters by Value and its a call by value language.

Full Disclosure: This post may contain affiliate links, meaning that if you click on one of the links and purchase an item, we may receive a commission (at no additional cost to you). We only hyperlink the products which we feel adds value to our audience. Financial compensation does not play a role for those products.

About Sanjeev

Sanjeev is an IT Consultant and technology enthusiast. He has more than 15 years of experience in building and maintaining enterprise applications. He is been with Android from T-Mobile G1 time but recently shifted to iOS. He loves to code and play with the latest gadgets.

Share via
Copy link