• ALL
  • CATEGORIES

Parameter Passing in Java

One of the first things we learn about a programming language we just laid hands on is the way it manages the parameters when we call a method. The two most common ways that languages deal with this problem are called “passing by value” and “passing by reference”.

Passing by value means that, whenever a call to a method is made, the parameters are evaluated, and the result value is copied into a portion of memory. When the parameter is used inside the method, either for read or write, we are actually using the copy, not the original value which is unaffected by the operations inside the method.

On the other hand, when a programming language uses passing by reference, the changes over a parameter inside a method will affect the original value. This is because what the method is receiving is the reference, i.e the memory address, of the variable.

Some programming languages support passing by value, some support passing by reference, and some others support both. So the question is, what does Java support?

Does Java pass parameters by value or by reference?

The answer to this question can be a little controversial, as there is some misunderstanding around how Java works this out. A lot of developers have the wrong idea that Java treats primitives and objects differently, so you often hear things like “Java passes primitives by value and object by reference”. Although, this is not entirely true. The reality is that Java always passes parameters by value.

At this point, we could wonder where the confusion comes from. The explanation is that Java does things in its own special way.

If we look at how Java treats primitive parameters, it is clearly passing them by value, as there is nothing like a reference to a primitive in Java. But what happens for objects? Think about this snippet:

 

public static void main (String[] args) {

Person aPerson = new Person(“Alice”);

myMethod(aPerson);

System.out.println(aPerson.getName());

}

public static void myMethod(Person person) {

person.setName(“Bob”);

}

It will print “Bob”, hence the original variable has been modified. If we go back to the definition of “passing by reference”, we can deduce that what is happening is that Java is passing a reference to the variable aPerson, therefore any modification inside the method affects the original variable.

Let’s make a small change on our method code:

 

public static void myMethod(Person person) {

person = new Person(“Bob”);

}

If the assumption we just made is right, and any modification on the parameter inside the method affects the original variable, this should print the same result as before (“Bob”). Although, if we execute this, the result is “Alice”.

Java passes the reference of the object by value

So what is happening here? What Java really does with objects is pass the reference of the object by value. This is completely different from the proposition “Java passes object by reference”.

When we define something like Person aPerson, we are not defining an object Person itself, but a pointer to an object Person. When calling a method with aPerson as a parameter, Java doesn’t copy the object, copies the reference to the object. If we look back to the definition of passing by value, it matches this behaviour; we are copying the original value and passing the copy to the method. But remember, the original value is not the object itself but a reference to the object.

When inside the method we do person.setName(“Bob”), we are accessing the referenced object and changing it. But when we do person = new Person(“Bob”), what we are changing is the reference to the object, and remember that this reference is not the original, it is a copy, hence the original reference doesn’t get affected.

So, in summary, Java always passes parameter by value for both, primitives and object. When dealing with object, it passes the reference of the object by value, and not the object itself.

I hope this article helps you to have a better understanding on how Java works and to avoid confusion around the passing of parameters.

Leave a reply

You can use these tags:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  1. Socaciu Mihai says:

    Thank you very much! This is a great explanation! Every popular language should have a similar article (like Python, C).

Sign up for the Manifesto newsletter and exclusive event invites