Argument passing nature of JAVA

If you are coming from C/C++ programming background, you probably heard about “Call by Value” and “Call by Reference”  argument passing mechanisms.

Briefly,

  • Call by Value : A copy of the object instance is passed to called method. Any modifications made on the object inside the method will not be reflected back to the original object.
  • Call by Reference : A reference to  the original object is passed to called function. This reference may be thought as an alias for the original object and any modification done on the alias object inside the method actually done on the original object.

In this sense what about JAVA?

In Java, parameters are passed to the methods by object value. Another words it is “call by value” but there is a trick while passing complex object types.

First of all, let’s start with simple object types and try to change their values in a method.

[java]
public static void main(String[] args){
String someString = "someString";
Integer someInteger = 25;
char someChar = ‘a’;
int someInt = 5;
System.out.println("Before method call : " + someString + " " + someInteger + " " + someChar + " " + someInt);
changeValues(someString, someInteger, someChar, someInt);
System.out.println("After method call : " + someString +" "+someInteger+" "+someChar+" "+someInt);
}

private static void changeValues(String anyString, Integer anyInteger, char anyChar, int anyInt) {
anyString = "anotherString";
anyInteger = 55;
anyChar = ‘b’;
anyInt = 6;
}
[/java]

And the result is :

[shell]
Before method call : someString 25 a 5
After method call : someString 25 a 5
[/shell]

In Java,  we already know that wrapper objects like strings and integers are immutable, and as it is shown, the values of the primitive types remains same after the method call. This result is compliant with the call by value definition.

Let’s see how complex objects are passing.

Suppose, we have an object called Dog with a simple name property.

[java]
public class Dog {
private String name;

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "dog name = " + name;
}
}
[/java]

First we initialize our dog object and give it a name “Max”. Then we pass this dog object to changeMyDogName method in order to change dog name.

[java]
public static void main(String[] args){
Dog myDog = new Dog();
myDog.setName("Max");
System.out.println("Before method call : " + myDog.toString());
changeMyDogName(myDog);
System.out.println("After method call : " + myDog.toString());
}

private static void changeMyDogName(Dog dog) {
dog.setName("Toby");
dog = new Dog();
dog.setName("Jake");
}
[/java]

And the result is :

[shell]
Before method call : dog name = Max
After method call : dog name = Toby
[/shell]

As you can see, the dog name is changed to “Toby” after method call. You may think if all object values are copied, the dog name should not be changed and it should remain as “Max”. The trick is that, what is copied to the method is not the object values but the object reference. You may also think, if object reference is copied to the method, why the dog name was not changed to “Jake” as we created a new dog object and set its name to “Jake”. In order to answer this question let me visualize it.

First we created a dog with a name “Max”.  Lets say it has a red lead like the following picture. When we call changeMyDogName we are actually passing this red lead.

Max
Max

As soon changeMyDogName method is initialized. The red lead is copied. Let’s say it is copied as a blue lead as the following picture.

Max
Max


At that point, both blue and red leads are fastened to original dog. So whatever done to this dog is actually done to the original dog. This is the reason why dog name is changed to “Toby”.

On the other hand, when we create a new dog in the method, we are actually creating a new dog.
At that point, blue lead is now fastened to a completely different dog(Jake) so whatever done to new dog does not affect the original dog.

Jake
Jake
Toby
Toby (It was Max)

As a result,  Java is call by value.  As described above, while passing complex objects, the passed value is a copy of the object reference.

 

disclaimer : The lovely dog picture is taken from www.groomers-online.com