cyberangles blog

Java Function Arguments: Does Modifying a Boolean Parameter's Value Affect the Original Variable?

In Java, understanding how function arguments are passed is critical for writing predictable and bug-free code. A common source of confusion among developers—especially those new to Java—is whether modifying a parameter’s value inside a method affects the original variable passed from the caller. This question becomes even more nuanced when dealing with boolean values, which can exist as either primitive types (boolean) or object types (Boolean).

In this blog, we’ll demystify Java’s parameter-passing behavior, specifically focusing on boolean values. We’ll explore whether modifying a boolean parameter (primitive or object) inside a method changes the original variable, why this behavior occurs, and what alternatives exist if you need to propagate changes.

2026-01

Table of Contents#

  1. Understanding Java’s Parameter-Passing Mechanism
  2. What is a "Boolean" in Java?
  3. Experiment 1: Modifying a Primitive boolean Parameter
  4. Experiment 2: Modifying a Boolean Object Parameter
  5. Why Doesn’t Modifying a Boolean Parameter Affect the Original?
  6. Common Misconceptions
  7. Practical Implications: How to "Modify" a Boolean Externally
  8. Conclusion
  9. References

1. Understanding Java’s Parameter-Passing Mechanism#

Before diving into boolean-specific behavior, we must first clarify how Java passes arguments to methods. Java is strictly pass-by-value, not pass-by-reference. This means:

  • When you pass a variable to a method, the method receives a copy of the variable’s value, not the variable itself.
  • Changes made to the copied value inside the method have no effect on the original variable in the caller.

This rule applies to all types, but the behavior differs slightly for primitives vs. objects:

  • Primitive types (e.g., int, boolean, double): The method receives a copy of the primitive’s value. Modifying this copy leaves the original primitive unchanged.
  • Object types (e.g., String, Boolean, ArrayList): The method receives a copy of the reference (memory address) pointing to the object. While you can modify the state of the object (e.g., adding elements to an ArrayList), reassigning the copied reference to a new object will not affect the original reference in the caller.

2. What is a "Boolean" in Java?#

In Java, "boolean" can refer to two distinct types:

2.1 Primitive boolean#

The primitive type boolean has only two possible values: true or false. It is not an object, and it does not have methods or fields. Example:

boolean isActive = true; // Primitive boolean

2.2 Wrapper Class Boolean#

The Boolean class is a wrapper for the primitive boolean type. It is an immutable object (its value cannot be changed after creation) and provides utility methods (e.g., parseBoolean(), valueOf()). Example:

Boolean isActiveObj = Boolean.TRUE; // Boolean object (immutable)

Key note: Boolean is immutable, meaning you cannot modify its internal value. Once created, a Boolean object remains true or false forever.

3. Experiment 1: Modifying a Primitive boolean Parameter#

Let’s test whether modifying a primitive boolean parameter affects the original variable.

Code Example:#

public class BooleanParameterTest {
    public static void main(String[] args) {
        boolean originalPrimitive = true;
        System.out.println("Before method call: originalPrimitive = " + originalPrimitive);
 
        // Pass the primitive boolean to a method that tries to modify it
        modifyPrimitiveBoolean(originalPrimitive);
 
        System.out.println("After method call: originalPrimitive = " + originalPrimitive);
    }
 
    private static void modifyPrimitiveBoolean(boolean param) {
        // Try to change the parameter's value
        param = false;
        System.out.println("Inside method: param = " + param);
    }
}

Output:#

Before method call: originalPrimitive = true
Inside method: param = false
After method call: originalPrimitive = true

Explanation:#

  • The main method declares originalPrimitive as true and passes it to modifyPrimitiveBoolean.
  • The modifyPrimitiveBoolean method receives a copy of originalPrimitive’s value (true). It then reassigns this copy to false.
  • The original variable originalPrimitive in main remains true because the method only modified the copied value, not the original.

4. Experiment 2: Modifying a Boolean Object Parameter#

Now let’s test the wrapper class Boolean. Since Boolean is an object, we might expect different behavior—but remember, Boolean is immutable.

Code Example:#

public class BooleanObjectTest {
    public static void main(String[] args) {
        Boolean originalObject = Boolean.TRUE; // Wrapper object
        System.out.println("Before method call: originalObject = " + originalObject);
 
        // Pass the Boolean object to a method that tries to modify it
        modifyBooleanObject(originalObject);
 
        System.out.println("After method call: originalObject = " + originalObject);
    }
 
    private static void modifyBooleanObject(Boolean param) {
        // Try to reassign the parameter to a new Boolean value
        param = Boolean.FALSE;
        System.out.println("Inside method: param = " + param);
    }
}

Output:#

Before method call: originalObject = true
Inside method: param = false
After method call: originalObject = true

Explanation:#

  • The main method creates a Boolean object originalObject initialized to true and passes it to modifyBooleanObject.
  • The method receives a copy of the reference to originalObject. It then reassigns this copied reference to point to Boolean.FALSE (a new Boolean object).
  • The original originalObject reference in main still points to the original Boolean.TRUE object. Reassigning the copied reference inside the method does not affect the original reference.

Why can’t we modify the Boolean object’s value directly? Because Boolean is immutable—there are no setter methods to change its internal value. Even if you could, the copy of the reference would prevent changes to the original object.

5. Why Doesn’t Modifying a Boolean Parameter Affect the Original?#

The behavior observed in both experiments stems from two core Java principles:

5.1 Pass-by-Value for Primitives#

For primitive boolean, the method receives a copy of the value. Changes to the copy are local to the method and do not propagate back to the caller.

5.2 Immutable Boolean Objects + Pass-by-Value for References#

For Boolean objects:

  • The method receives a copy of the reference (memory address) to the Boolean object.
  • Since Boolean is immutable, you cannot modify its value—you can only reassign the copied reference to a new Boolean object.
  • Reassigning the copied reference has no effect on the original reference in the caller.

6. Common Misconceptions#

Misconception 1: "Java uses pass-by-reference for objects."#

False. Java uses pass-by-value for all types. For objects, the "value" passed is a copy of the reference. While you can modify the state of the object (e.g., list.add("item") for an ArrayList), reassigning the reference (e.g., list = new ArrayList()) does not affect the original object.

Misconception 2: "Wrapper classes like Boolean can be modified."#

False. All wrapper classes (Boolean, Integer, String, etc.) are immutable. Their values cannot be changed after creation.

7. Practical Implications: How to "Modify" a Boolean Externally#

If you need a method to modify a boolean value and have the change reflected in the caller, you cannot use a primitive boolean or Boolean object directly. Instead, use one of these workarounds:

7.1 Return the Modified Value#

The simplest solution: have the method return the new boolean value and reassign it to the original variable.

Example:

public class ReturnBooleanExample {
    public static void main(String[] args) {
        boolean original = true;
        original = toggleBoolean(original); // Reassign with returned value
        System.out.println("Original after toggle: " + original); // Output: false
    }
 
    private static boolean toggleBoolean(boolean param) {
        return !param; // Return modified value
    }
}

7.2 Use a Mutable Wrapper Class#

Create a custom mutable class to hold the boolean value, or use Java’s built-in AtomicBoolean (from java.util.concurrent.atomic).

Custom Mutable Wrapper:#

class MutableBoolean {
    private boolean value;
 
    public MutableBoolean(boolean value) {
        this.value = value;
    }
 
    public boolean getValue() { return value; }
    public void setValue(boolean value) { this.value = value; }
}
 
public class MutableBooleanExample {
    public static void main(String[] args) {
        MutableBoolean wrapper = new MutableBoolean(true);
        modifyWrapper(wrapper);
        System.out.println("Wrapper value after modification: " + wrapper.getValue()); // Output: false
    }
 
    private static void modifyWrapper(MutableBoolean wrapper) {
        wrapper.setValue(false); // Modify the wrapper's internal state
    }
}

AtomicBoolean (Built-in Mutable Wrapper):#

import java.util.concurrent.atomic.AtomicBoolean;
 
public class AtomicBooleanExample {
    public static void main(String[] args) {
        AtomicBoolean atomicBool = new AtomicBoolean(true);
        modifyAtomicBoolean(atomicBool);
        System.out.println("AtomicBoolean value after modification: " + atomicBool.get()); // Output: false
    }
 
    private static void modifyAtomicBoolean(AtomicBoolean atomic) {
        atomic.set(false); // Modify the AtomicBoolean's value
    }
}

7.3 Use a Boolean Array#

Arrays are objects, so passing a boolean array allows modification of its elements (since the array reference is copied, but the array itself is shared).

Example:

public class BooleanArrayExample {
    public static void main(String[] args) {
        boolean[] boolArray = {true}; // Array with one boolean element
        modifyBooleanArray(boolArray);
        System.out.println("Array value after modification: " + boolArray[0]); // Output: false
    }
 
    private static void modifyBooleanArray(boolean[] array) {
        array[0] = false; // Modify the array's element
    }
}

8. Conclusion#

In Java, modifying a boolean (primitive) or Boolean (object) parameter inside a method does not affect the original variable in the caller. This behavior is explained by:

  • Java’s strict pass-by-value parameter passing mechanism.
  • The immutability of the Boolean wrapper class.

To propagate boolean changes from a method to the caller, use workarounds like returning the modified value, mutable wrappers (e.g., AtomicBoolean), or arrays. Understanding these concepts ensures you write predictable and bug-free Java code.

9. References#