Last Updated: January 3, 2026
Understanding how Java handles method parameters is crucial to becoming an effective developer.
One of the most fundamental concepts in this area is pass by value. It shapes how data behaves when passed to methods, which can lead to unexpected surprises if you're not aware of how it works. So, let’s dive deep into this concept and unravel its intricacies.
At its core, pass by value means that when you pass a variable to a method, you are passing a copy of that variable's value, not the variable itself. This is true for both primitive types and object references in Java.
When you pass a primitive type (like int, float, or char), the method receives a copy of that value. Any changes made to that parameter in the method do not affect the original variable.
In the example above, modifyValue attempts to change value, but it only changes the copy. The original originalValue in main remains unaffected.
When dealing with objects, the concept of pass by value might seem a bit more complex. While you are still passing a copy of the variable, in this case, it’s a copy of the reference to the object, not the actual object. This means that although the reference itself is a copy, it still points to the same object in memory.
Here, modifyObject changes the state of the object that myObject refers to. While the reference to myObject is passed by value, both myObject and obj point to the same instance of MyClass. So, any modification affects the original object.
Java has several immutable classes, such as String. When you pass an immutable object to a method and try to modify it, it might appear as if you are changing the original object, but in reality, you are creating a new instance.
In this case, even though modifyString tries to concatenate ", World!" to str, it doesn't modify original. Instead, it creates a new String object, leaving the original string untouched.
Understanding immutability is key when working with Java objects. Always remember that modifications to immutable objects do not change the original instance.
Developers new to Java often run into misunderstandings about how pass by value works, particularly with references. Here are some common pitfalls:
In this example, even though we created a new MyClass object and assigned it to obj, myObject remains unchanged, as the original reference is unaffected by this reassignment.
Understanding pass by value is crucial in crafting robust applications. Here are some real-world scenarios where this knowledge becomes particularly valuable: