What is call by reference and call by value?

📤 Call by Value (Java uses this)

When a method is called, a copy of the variable’s value is passed. So, any changes made inside the method do not affect the original variable.

Example:

Even though x was changed inside the method, the original variable a remains unchanged.

🔁 Call by Reference (Java mimics this with objects)

Here, a reference (or address) to the actual variable is passed, so changes made inside the method do affect the original object.

Example:

The array’s first element changed! That’s because the method worked on the actual array through its reference.

🧠 Quick Summary:

  • Call by value: You give someone a copy of your house key (primitives).
  • Call by reference: You give someone the actual key (objects).

Leave a Reply

Your email address will not be published. Required fields are marked *