Fundamentals 9 min read

Understanding Java Parameter Passing, Autoboxing, and Swapping Integer Values

This article explains why swapping Integer objects using a simple assignment fails in Java, covering formal vs. actual parameters, value versus reference passing, autoboxing mechanics, the Integer cache, and a reflective workaround that avoids the cache to correctly exchange values.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Understanding Java Parameter Passing, Autoboxing, and Swapping Integer Values

The article starts by presenting a typical Java interview question that attempts to swap two Integer variables using a naive method, which does not work because the method only swaps the local references.

It then reviews fundamental concepts such as formal (parameter) and actual (argument) parameters, explaining that arguments are passed by value for primitives and by reference for objects, but the reference itself is still passed by value.

Next, the difference between value passing and reference passing is clarified, showing that changing the object’s internal state affects the original object, while reassigning the reference does not.

The article introduces autoboxing, noting that the compiler translates literals like Integer a = 1; into calls to Integer.valueOf(1) , which may return cached objects for small values.

It demonstrates two swap implementations: swap1 , which only swaps the local references and therefore has no effect, and swap2 , which modifies the objects via setter methods, successfully swapping the values.

Since Integer has no setter, the author attempts a reflective solution that accesses the private final value field. The initial reflective code fails because the temporary variable is autoboxed, causing the cache to be altered.

By creating a new Integer instance for the temporary value (avoiding the cache), the reflective swap works correctly, as shown in the final code snippet.

Finally, the article concludes that understanding these concepts prevents common mistakes in interview coding questions.

public static void main(String[] args) {
    Integer a = 1;
    Integer b = 2;
    System.out.printf("a = %s, b = %s\n", a, b);
    swap(a, b);
    System.out.printf("a = %s, b = %s\n", a, b);
}

public static void swap(Integer a, Integer b) {
    Integer temp = a;
    a = b;
    b = temp;
}
public static void swap(Integer a, Integer b) {
    int temp = a.intValue();
    try {
        Field value = Integer.class.getDeclaredField("value");
        value.setAccessible(true);
        value.set(a, b);
        value.set(b, new Integer(temp));
    } catch (NoSuchFieldException | IllegalAccessException e) {
        e.printStackTrace();
    }
}
JavareflectionInterviewautoboxingparameter passingswap
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.