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.
3 points to say:
- Changing the object's content: the caller sees it.
- Pointing the parameter at another object: the caller doesn't.
- 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.