StackIsLife

Java Is Pass By Value

September 17, 2019

The question of java pass by value or pass by reference is a very common interview question. Personally, I know very experienced java developers who answer this question very differently. The short answer is that java is always pass by value, but this won’t help with interview questions - you need to know why.

When we say pass by value, what do we mean? Values correspond to actual data. For example, the integer 3 is stored as 0011 in memory. Integers, doubles, booleans, and others are referred to as primitive values. Java would be very slow if it stored everything as an object, so these are stored in a faster format.

Objects are just containers that hold values or other references to objects. When we store an object in memory, we want to access it quickly. We also don’t know how large the object will be as an objects hold different amounts of information. For example, a BigInt has no theoretical limit in size. Thus, we store a reference to the object on the stack, and the actual values of the object in the heap. References are just values that refer to other objects or primitive values. You can create an object and print it out below.

public class MyObject {
     public MyObject(){}
}

public class App {
     public static void main(final String args[]){
          MyObject myObject = new MyObject();
          System.out.print(myObject);
     }
}

-------
MyObject@6e0be858

This @6e0be858 is a memory location or address. It’s called a reference, but can also be called a pointer. Java, however, doesn’t have pointers in the traditional sense. It has much stricter memory management, and so you can’t do arithmetic and guarantee any kind of result; the JVM handles pointers and memory management for you.

This is an important detail though, “references” can sometimes behave much more like pointers than they do as references in any other language. Here’s a link to a great infographic detailing the difference between pass by value and pass by reference:

Pass by value vs pass by reference animation

The truth is that java falls somewhere in between pass by value and pass by reference. Because functions are always passing pointers to objects, you can still affect the original object that is passed into the function. However, you can’t affect the original pointer outside of the function.

Cat gerald = new Cat(Gerald);

changeCat(gerald);

private changeCat(Cat oldCat){
     Cat steve = new Cat(Steve);
     oldCat = steve;
}

//Our new cat is still gerald
System.out.println(gerald.getName());

------Gerald

Our swap didn’t work because we passed a reference/pointer to the changeCat function. This means we passed the memory location (i.e. @5e0be111) to the changeCat function. This is a plain old value. So what we did was similar to the code below:

int five = 5;
changeInt(five);
private changeInt(int oldInt){
     int nine = 9;
     oldInt = nine;
}

System.out.println(five);

------
5

In languages like C++ where you do handle your own memory, there is a strong difference between pointers and references. In those languages, unless you dereference a pointer, it won’t change the original value. That’s what’s happening here. It’s much simpler to demonstrate with primitives as java will always pass a copy of the value to the function.

With objects, however, the distinction is a bit blurred. It will pass the value of the object’s reference below, but dereference the object automatically when you work with it. The below code is an anti-pattern as you should always return new objects from your functions as to avoid side-effects in your code:

Cat gerald = new Cat(Gerald);

changeCat(gerald);

private changeCat(Cat oldCat){
     oldCat.setName("Steve")
}

//We're changing our old cat's with a function that produces a side-effect
System.out.println(gerald.getName());

------Steve

What?! We changed the name of our cat via a side effect. Why you shouldn’t use side-effects is for another article. If you find an angry co-worker arguing over pass by reference/value for java, please point them to this article.

Link that inspired the post: Processing an unsorted array


Written by Scott Hansen who works in New York City building great stuff.