This is one of those concepts that seems obvious… until it breaks your code.
Many developers believe:
“Objects are passed by reference in Java or JavaScript”
❌ That’s incorrect.
👉 Both Java and JavaScript are strictly call by value
🧠 Why This Feels Wrong
If you’ve ever done this:
function update(user):
user.name = "Ishwar"
…and seen the change reflected outside,
it feels like pass by reference.
But then this:
function reset(user):
user = new Object()
Does nothing to the original.
So what’s really going on?
⚙️ What’s Actually Happening
- Variables store values
- For objects → that value is a reference (memory address)
👉 When calling a function:
- A copy of that value is passed
- Not the original variable
🔍 The Key Insight
Inside the function:
- You get a copy of the reference
- Both point to the same object → mutation works
- But reassignment only affects the local copy
🔁 Mental Model
original variable → copy value → function parameter
👉 You never get direct access to the original variable
📦 Stack vs Heap (Quick View)
- Stack → variables (copies live here)
- Heap → actual objects
👉 You copy the reference value, not the object
🌍 Applies Beyond Just Java
| Language | Behavior |
|---|---|
| Java | Call by Value |
| JavaScript | Call by Value |
| Python | Call by Value (object ref) |
| C# | Call by Value (default) |
💡 Final Takeaway
Everything is pass by value.
Some values just happen to be references.
🚀 Why This Matters
This concept shows up in real-world bugs:
- Unexpected object mutations
- State issues in frontend apps
- Backend data inconsistencies
- API transformation bugs
🤔 Discussion
When did this finally click for you?
- Early learning phase
- On the job
- Or after debugging something painful 😅
Built for devs who like understanding why, not just what.
More deep dives coming.
Top comments (0)