NBKRIST JavaHub

Passing Arguments in Java – Value & Reference

Java handles data passing in a unique but consistent way. This page explains one of the most misunderstood topics: Pass by Value and Objects as Parameters.

1. Passing Arguments by Value

Java is strictly pass-by-value. This means a copy of the variable is passed to methods. Modifying the copy does NOT affect the original value.

class Test { void change(int x) { x = 50; } public static void main(String[] args) { int a = 10; new Test().change(a); System.out.println(a); } }
Output: 10 (Original value unchanged)

2. Class Objects as Parameters (Looks Like Reference)

Objects behave differently because the value passed is a copy of the reference. Both references point to the same object, so modifying the object reflects outside the method.

class Car { int speed; } class Demo { void modify(Car c) { c.speed = 100; } public static void main(String[] args) { Car car = new Car(); car.speed = 40; new Demo().modify(car); System.out.println(car.speed); } }
Output: 100 (Object modified!)

Java does NOT pass objects by reference. It passes a copy of the reference → but the referenced object is the same.

Practice Questions – Click to Reveal Answers

1️⃣ What does Java pass: value or reference?
Java always passes by value. For objects, it passes a copy of the reference.
2️⃣ Why does modifying an object inside a method reflect outside?
Because the reference copy still points to the same object in memory.
3️⃣ Will reassigning an object inside a method affect the original?
No. Reassignment changes only the local copy of the reference.

Quick Summary