Call by Value vs Call by Reference in Java (ICSE Guide)

⚡ Direct Answer & Key Takeaway

In ICSE Java, Call by Value passes a copy of a primitive argument to a method, leaving the original variable unchanged in the caller. Passing an array or object passes a copy of its memory reference—commonly termed Call by Reference in CISCE textbooks—allowing the method to mutate shared elements or fields directly. Pure methods read arguments without altering caller state, whereas impure methods modify passed objects or array elements permanently.

1. How Primitive Arguments Behave Under Call by Value

When you invoke a method with an int, double, or char argument, the Java Virtual Machine allocates a separate stack frame and copies the raw value into the formal parameter. Any arithmetic reassignment—such as x = x + 50—updates only that local formal copy inside the called method. Once the method returns, its stack frame is popped and discarded, so the actual parameter in main() retains its original value. CISCE Section A frequently tests this by naming the formal and actual parameters identically, tricking unprepared students into thinking both variables share one memory slot.

  • Actual parameters belong to the caller's stack frame; formal parameters belong to the called method's frame.
  • Swapping two primitive int variables inside a void swap(int a, int b) helper leaves the caller's variables unswapped.
  • To persist a primitive change, return the updated value and assign it back in main().

2. Array and Object Mutations (ICSE Call by Reference)

Passing an array or class object behaves differently because the variable itself holds a heap memory address rather than raw data. Copying that reference into a formal parameter gives both main() and the helper method access to the exact same heap array or object fields. Consequently, writing arr[0] = arr[0] + 10 inside an impure method alters the original array visible back in main(). However, reassigning the reference itself inside the method (arr = new int[3];) only redirects the local parameter and leaves the caller's original reference untouched.

  • Arrays and mutable user-defined objects allow element or field mutations to persist after the method returns.
  • String objects are immutable; modifying a String parameter inside a method creates a new local String without changing the caller's reference.
  • Use array-based swaps such as swap(int[] arr, int i, int j) when sorting methods must mutate data in place.

3. Pure vs. Impure Methods in Section A Board Questions

CISCE syllabi connect parameter passing directly to pure and impure functions. A pure method receives arguments, computes a result without changing any passed object or instance field, and returns a value—making it ideal for Call by Value primitives. Conversely, an impure (mutator) method modifies the state of a passed array or object in place and often uses a void return type. On two-mark definition questions, always state both the parameter copy rule and an authentic Java example pair such as Math.sqrt(n) versus an array-sorting helper.

  • Pure methods produce zero side effects and always return the same output for identical inputs.
  • Impure methods alter instance variables, static variables, or passed array/object contents.
  • In 2-mark ICSE comparisons, contrast 'copy of data vs. reference address' and 'primitives vs. arrays/objects'.

4. ParameterPassingDemo.java (Call by Value vs Array Reference Mutation)

import java.util.*;

public class ParameterPassingDemo {
    // Pure method: Call by Value on primitive int
    public static void addBonusValue(int bonus) {
        bonus = bonus + 500;
        System.out.println("Inside addBonusValue (formal copy): " + bonus);
    }

    // Impure method: Reference copy mutates caller's array
    public static void addGraceMarks(int[] marks, int grace) {
        for (int i = 0; i < marks.length; i++) {
            marks[i] = marks[i] + grace;
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter initial bonus (primitive): ");
        int bonus = sc.nextInt();

        int[] marks = new int[3];
        System.out.print("Enter 3 subject marks: ");
        for (int i = 0; i < marks.length; i++) {
            marks[i] = sc.nextInt();
        }

        addBonusValue(bonus);
        System.out.println("In main after Call by Value: " + bonus);

        addGraceMarks(marks, 5);
        System.out.print("In main after array reference mutation: ");
        for (int i = 0; i < marks.length; i++) {
            System.out.print(marks[i] + (i < marks.length - 1 ? ", " : "\n"));
        }
    }
}

Verified Console / Dry-Run Output:

Enter initial bonus (primitive): 1000
Enter 3 subject marks: 78 84 91
Inside addBonusValue (formal copy): 1500
In main after Call by Value: 1000
In main after array reference mutation: 83, 89, 96

5. CISCE Variable Description Table (VDT) & Dry Run

VariableData TypeExam Purpose & Sample Value
bonusintPrimitive passed by value; stays 1000 in main while formal copy becomes 1500
marksint[]Array reference passed to method; mutates [78, 84, 91] into [83, 89, 96]
graceintFixed increment added to every element inside addGraceMarks; sample: 5
iintLoop counter iterating 0 to 2 over marks array
scScannerReads primitive bonus and 3 array marks from System.in
🎯 1-on-1 Live Mentorship & Free Study Kit

Trace Java Memory & Methods Without Guesswork

Walk through stack vs. heap memory diagrams and Section A output-tracing traps with an expert ICSE Java tutor in a 1-on-1 session.

💬 WhatsApp Now
🔒 Instant WhatsApp notes + free 60-min 1-on-1 trial session
Confirmed! Our Senior Mentor is sending your study kit & free 1-on-1 demo link on WhatsApp now.

6. Common Mistakes & Top Scorer Tips

  • Avoid Mistake: Assuming that matching formal and actual parameter names (such as bonus in both main and helper) cause changes to reflect back in main().
  • Avoid Mistake: Claiming Call by Reference changes a String argument in Java; Strings are immutable, so reassigning s = "New" only points the local parameter to a new String object.
  • Avoid Mistake: Expecting arr = new int[3]; inside a method to replace the caller's array reference, instead of mutating elements via arr[i].
  • Examiner / Mentor Tip: In Section A dry-run tables, draw separate columns for main()'s variables and the method's formal parameters to avoid mixing values.
  • Examiner / Mentor Tip: When asked to define Call by Reference in an ICSE paper, explicitly mention arrays or objects as the parameter type.
  • Examiner / Mentor Tip: Remember that Math library functions (Math.pow, Math.abs) are classic board examples of pure methods.

7. Interactive Concept Check Quiz

Question: Given void modify(int x, int[] a) { x = x * 2; a[0] = a[0] + 5; }, if main() calls modify(x, arr) with x = 10 and arr[0] = 4, what are x and arr[0] afterward in main()?

8. Frequently Asked Questions

Does Java truly support Call by Reference like C++ pointers?
Strictly speaking, Java passes everything—including object references—by value. However, because the copied value for an object or array is its heap address, the called method can modify the object's internal fields or array elements. ICSE textbooks label this reference-passing mechanism 'Call by Reference' to contrast it with primitive value passing.
If formal and actual parameters have the same name, does Call by Value change the original?
No. Identical names like int n in both main() and update(int n) still occupy completely separate memory locations in different method stack frames. Modifying n inside update() never alters n inside main() unless update() returns the new value and main() explicitly reassigns it with n = update(n).
What distinguishes a pure method from an impure method in ICSE Class 10?
A pure method returns a value computed solely from its inputs without modifying any instance variables, static variables, or passed array elements. An impure method changes external state—such as mutating an array in place or incrementing an object field—and usually exhibits side effects visible after the method returns.

Explore Related Lessons & Free Interactive Tools

Free 1-on-1 Session

Book a Free Demo Class