SteelReed

Search

Search posts, questions…

Java CoreQuestion 8 / 43

Junior

Is Java pass-by-value or pass-by-reference?

Short answer

Java always passes by value (it copies the value into the method). For objects, the copied value is the object's address.

same addresssame addresslist = new ArrayListnames: caller's slotArrayList on heaplist: method's slotNew ArrayListcaller never sees

3 points to say:

  1. Changing the object's content: the caller sees it.
  2. Pointing the parameter at another object: the caller doesn't.
  3. That is why you can't write a swap(a, b) method.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.