← Back to Home

Call by Value

Call by Value means that Java passes a copy of the value to a method, not the original variable itself. Any changes made to the parameter inside the method do not affect the original variable in the caller. This is a frequently asked interview topic, especially to clarify the confusion around objects and references.

Is Java Call by Value or Call by Reference?

Java is strictly CALL BY VALUE. Always.

  • For primitives → copy of the value is passed
  • For objects → copy of the reference is passed
  • The original reference itself is never passed

Call by Value with Primitive Types

Example

void change(int x) {
    x = 50;
}
public static void main(String[] args) {
    int a = 10;
    change(a);
    System.out.println(a);
}
          

Output:

10
          

Explanation

  • a = 10
  • Copy of a is passed to x
  • Changes to x do NOT affect a

Call by Value with Object References (Very Important)

Example

void change(int[] arr) {
    arr[0] = 100;
}
public static void main(String[] args) {
    int[] a = {10, 20};
    change(a);
    System.out.println(a[0]);
}
          

Output:

100
          

Explanation

  • Copy of reference a is passed
  • Both references point to the same object
  • Object content is modified
  • Reference itself is not changed

Why This Is Still Call by Value

void change(int[] arr) {
    arr = new int[]{50, 60};
}

int[] a = {10, 20};
change(a);
System.out.println(a[0]); // 10
          

Explanation

  • Reference copy is reassigned inside method
  • Original reference a remains unchanged
  • Confirms Java is not call by reference

Visual Understanding (Conceptual)

Primitive

a = 10
copy -> x = 10
          

Object

a ----> [10, 20]
copy of reference ----> [10, 20]
          

Both references point to same object, but reference itself is copied.

Common Misconception

  • ❌ Java supports call by reference
  • ❌ Objects are passed by reference
  • ✔ Java passes everything by value
  • ✔ For objects, the value is the reference

Call by Value vs Call by Reference (Conceptual)

Feature Call by Value (Java) Call by Reference
What is passed Copy of value Original reference
Affects caller variable ❌ No ✅ Yes
Java supports ✅ Yes ❌ No

Why Java Uses Call by Value

  • Simpler and safer
  • Avoids unintended side effects
  • Improves security and predictability
  • Easier memory management

Common Beginner Mistakes

  • Expecting primitive values to change
  • Thinking Java is call by reference
  • Confusing object mutation with reference change
  • Not understanding reference copying

Interview-Ready Answers

Short Answer

Java uses call by value, meaning a copy of the value is passed to methods.

Detailed Answer

In Java, all method arguments are passed by value. For primitive types, a copy of the actual value is passed. For objects, a copy of the reference is passed, allowing object modification but not reference reassignment. Java does not support call by reference.

Key Takeaway

Java is 100% call by value. Understanding how primitive values and object references are passed prevents bugs and clears one of the most common interview traps.

Additional Call by Value Examples

1. Call by Value with Primitive Type

class Demo {
    static void change(int x) {
        x = 100;
    }

    public static void main(String[] args) {
        int a = 10;
        change(a);
        System.out.println(a);
    }
}
          

Explanation

  • Copy of value passed
  • Original unchanged

Output:

10
          

2. Call by Value with Multiple Primitive Parameters

class Demo {
    static void modify(int a, int b) {
        a = 50;
        b = 60;
    }

    public static void main(String[] args) {
        int x = 5, y = 10;
        modify(x, y);
        System.out.println(x + " " + y);
    }
}
          

Explanation

  • Each parameter receives a copy

Output:

5 10
          

3. Call by Value with Object Reference (Mutable Object)

class Demo {
    static void change(StringBuilder sb) {
        sb.append(" Java");
    }

    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Hello");
        change(sb);
        System.out.println(sb);
    }
}
          

Explanation

  • Reference copy passed
  • Object content modified

Output:

Hello Java
          

4. Reassigning Object Reference Inside Method

class Demo {
    static void change(StringBuilder sb) {
        sb = new StringBuilder("New");
    }

    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Old");
        change(sb);
        System.out.println(sb);
    }
}
          

Explanation

  • Only local copy reassigned
  • Original object unaffected

Output:

Old
          

5. Call by Value with String (Immutable Object)

class Demo {
    static void change(String s) {
        s = s.concat(" Java");
    }

    public static void main(String[] args) {
        String s = "Hello";
        change(s);
        System.out.println(s);
    }
}
          

Explanation

  • New String created
  • Reference copy lost

Output:

Hello
          

6. Call by Value with Array Parameter

class Demo {
    static void change(int[] arr) {
        arr[0] = 99;
    }

    public static void main(String[] args) {
        int[] a = {1, 2, 3};
        change(a);
        System.out.println(a[0]);
    }
}
          

Explanation

  • Array object modified

Output:

99
          

7. Reassigning Array Reference Inside Method

class Demo {
    static void change(int[] arr) {
        arr = new int[]{7, 8, 9};
    }

    public static void main(String[] args) {
        int[] a = {1, 2, 3};
        change(a);
        System.out.println(a[0]);
    }
}
          

Explanation

  • Reference reassignment is local

Output:

1
          

8. Call by Value with Wrapper Class

class Demo {
    static void change(Integer x) {
        x = 100;
    }

    public static void main(String[] args) {
        Integer a = 10;
        change(a);
        System.out.println(a);
    }
}
          

Explanation

  • Wrapper is immutable

Output:

10
          

9. Wrapper Object Modification Attempt

class Demo {
    static void change(Integer x) {
        x = x + 10;
    }

    public static void main(String[] args) {
        Integer a = 20;
        change(a);
        System.out.println(a);
    }
}
          

Explanation

  • New Integer created

Output:

20
          

10. Call by Value with Custom Object (Field Change)

class User {
    int age;
}

class Demo {
    static void update(User u) {
        u.age = 30;
    }

    public static void main(String[] args) {
        User u = new User();
        u.age = 20;
        update(u);
        System.out.println(u.age);
    }
}
          

Explanation

  • Object field modified

Output:

30
          

11. Reassigning Custom Object Reference

class User {
    int age;
}

class Demo {
    static void update(User u) {
        u = new User();
        u.age = 40;
    }

    public static void main(String[] args) {
        User u = new User();
        u.age = 25;
        update(u);
        System.out.println(u.age);
    }
}
          

Explanation

  • New object created locally

Output:

25
          

12. Call by Value with null Reference

class Demo {
    static void test(String s) {
        s = "Java";
    }

    public static void main(String[] args) {
        String s = null;
        test(s);
        System.out.println(s);
    }
}
          

Explanation

  • Reference copy reassigned

Output:

null
          

13. Passing Expression as Argument

class Demo {
    static void show(int x) {
        System.out.println(x);
    }

    public static void main(String[] args) {
        show(5 + 3);
    }
}
          

Explanation

  • Expression evaluated first

Output:

8
          

14. Method Returning Modified Value

class Demo {
    static int change(int x) {
        x = 50;
        return x;
    }

    public static void main(String[] args) {
        int a = 10;
        a = change(a);
        System.out.println(a);
    }
}
          

Explanation

  • Return value must be captured

Output:

50
          

15. Misconception: “Call by Reference” Example

class Demo {
    static void swap(int a, int b) {
        int temp = a;
        a = b;
        b = temp;
    }

    public static void main(String[] args) {
        int x = 5, y = 10;
        swap(x, y);
        System.out.println(x + " " + y);
    }
}
          

Explanation

  • Swap fails due to call by value

Output:

5 10
          

16. Swap Using Object Wrapper

class Holder {
    int value;
}

class Demo {
    static void swap(Holder a, Holder b) {
        int temp = a.value;
        a.value = b.value;
        b.value = temp;
    }

    public static void main(String[] args) {
        Holder h1 = new Holder();
        Holder h2 = new Holder();
        h1.value = 5;
        h2.value = 10;

        swap(h1, h2);
        System.out.println(h1.value + " " + h2.value);
    }
}
          

Explanation

  • Object fields swapped

Output:

10 5
          

17. Call by Value with Varargs

class Demo {
    static void modify(int... nums) {
        nums[0] = 99;
    }

    public static void main(String[] args) {
        modify(1, 2, 3);
        // varargs creates array internally
    }
}
          

Explanation

  • Varargs treated as array
  • Underlying concept remains call by value

18. Call by Value in Method Chaining

class Demo {
    static int add(int x) {
        return x + 5;
    }

    public static void main(String[] args) {
        int result = add(add(5));
        System.out.println(result);
    }
}
          

Explanation

  • Each call passes copy

Output:

15
          

19. Interview Trick: Object vs Reference

class Demo {
    static void test(StringBuilder sb) {
        sb.append("X");
        sb = new StringBuilder("Y");
    }

    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("A");
        test(sb);
        System.out.println(sb);
    }
}
          

Explanation

  • Append affects original
  • Reassignment does not

Output:

AX
          

20. Interview Summary – Call by Value

class Demo {
    static void test(int x, StringBuilder sb) {
        x = 100;
        sb.append(" Java");
    }

    public static void main(String[] args) {
        int a = 10;
        StringBuilder sb = new StringBuilder("Hello");

        test(a, sb);

        System.out.println(a);
        System.out.println(sb);
    }
}
          

Explanation

  • Primitive unchanged
  • Object content changed

Output:

10
Hello Java