Stop 03 · Control Flow, Methods & ArraysTopics · 7/8

Pass-by-Value in Java

Part of the Java Roadmap

In short

"Is Java pass-by-reference?" is one of the most asked Java interview questions, and the honest answer is no. Java always passes a copy. The trick is knowing what is being copied when you pass an object.

Java always passes arguments by value. For objects, the value passed is a copy of the reference, so a method can change the object's contents but cannot make the caller's variable point to a new object.

The jaadu hands a friend a photocopied paper slip with a little house drawing on it, while the real house stands behind them unchanged.

Think of it like this

Sharing your home address

If you give a friend a photocopy of a number, they can scribble on it and your copy is untouched. If you give them a photocopy of your home address, they can visit and rearrange your furniture. But if they cross out the address on their slip and write another, your house does not move. Objects work the same: the reference is copied, not the house.

Watch it happen

What gets copied

main
method
Object on heap

copy of int 5

1/4Primitives: the method gets its own copy. Changing it does nothing to main's variable.

The big ideas

What you need to know

Always a copy

Java copies every argument into the parameter. There is no way to pass a variable itself.

Primitives are safe

A method can never change an int, double or boolean variable in the caller.

Objects share state

The copied reference points at the same object, so changes to its fields or contents are visible to the caller.

Reassignment is local

param = new ArrayList<>() inside a method does not affect the caller's variable.

See it in code

Walk through the code

java
1import java.util.ArrayList;2import java.util.List;3 4public class PassDemo {5 6    static void addBonus(int salary) {7        salary += 5000;8    }9 10    static void addItem(List<String> cart) {11        cart.add("Laddoo");12    }13 14    static void replaceCart(List<String> cart) {15        cart = new ArrayList<>();16        cart.add("Jalebi");17    }18 19    public static void main(String[] args) {20        int salary = 30000;21        addBonus(salary);22        System.out.println(salary);        // 3000023 24        List<String> cart = new ArrayList<>(List.of("Barfi"));25        addItem(cart);26        System.out.println(cart);          // [Barfi, Laddoo]27 28        replaceCart(cart);29        System.out.println(cart);          // [Barfi, Laddoo]30    }31}

1/4The int is copied. The method changes its own copy, so main still prints 30000.

Three methods that each try to change something. Only the one that edits the shared object succeeds.

Save yourself the pain

Common mistakes

Oops

Writing a swap(int a, int b) method and expecting the caller's values to swap.

Do this instead

It cannot work in Java. Return the new values, or swap elements inside an array or object.

Oops

Saying "objects are passed by reference" in an interview.

Do this instead

Say: Java is always pass-by-value; for objects, the value passed is a copy of the reference.

Your mission

Prove it yourself

~15 min

Quick check

After calling a method that does list = null; on its parameter, what happens to the caller's list variable?

Go deeper

  1. 1Write a swap method for two ints and see why it doesn't work.
  2. 2Change an array element inside a method and see the change outside.
  3. 3Reassign an object parameter inside a method and confirm the caller is unaffected.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge