Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

What does this print, and why?

Java
static void change(StringBuilder sb, int n, List<String> list) {
sb.append("!");
sb = new StringBuilder("new");
n = 5;
list.add("x");
list = new ArrayList<>();
}

called with sb = new StringBuilder("hi"), n = 1, list = new ArrayList<>(List.of("a")), then printing sb, n and list.

It prints hi!, then 1, then [a, x]. Java always passes arguments by value, and for an object the value that is copied is the reference, so the method can change the caller's object through it (append and add are visible) but reassigning the parameter only changes the method's own copy. The int is copied too, so n = 5 never reaches the caller.

Why this question is asked

Pass by value is one of the first things we learn in Java and one of the easiest to explain wrongly. Many developers say that objects are passed by reference, and that sentence leads to real bugs: methods that are expected to replace a caller's list but do not, or methods that were never meant to change a caller's data but do. Interviewers like this exact snippet because it mixes three cases in one method, a mutable object, a primitive and a collection, and each line tests the same rule. In this answer, we will recap what a reference is, trace the method line by line with a picture of memory, see why a swap method cannot work in Java, and finish with how to return more than one result instead.

1A Quick Recap of Variables and References

A variable of a primitive type, such as int, holds the number itself. A variable of a class type, such as StringBuilder or List, does not hold the object. It holds a reference, which is a value that points to an object living on the heap, the area of memory where objects are created. Two variables can hold the same reference, and then they point at one and the same object.

When we call a method, Java copies the value of each argument into the method's parameter. For an int, it copies the number. For a reference, it copies the reference, not the object. This is what pass by value means, and Java does it for every argument without exception. The method therefore starts with its own parameter variables that happen to point at the caller's objects.

2Tracing the Method Line by Line

Here is the program with the result of each line in the comments.

Java
static void change(StringBuilder sb, int n, List<String> list) {
sb.append("!"); // the caller's StringBuilder becomes "hi!"
sb = new StringBuilder("new"); // only the local sb now points to a new object
n = 5; // only the local copy of n changes
list.add("x"); // the caller's list becomes [a, x]
list = new ArrayList<>(); // only the local list now points to a new, empty list
}
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("hi");
int n = 1;
List<String> list = new ArrayList<>(List.of("a"));
change(sb, n, list);
System.out.println(sb); // hi!
System.out.println(n); // 1
System.out.println(list); // [a, x]
}
Who points where after change() returns
VariableBefore the callAfter the callWhy
main's sb-> StringBuilder "hi"-> same object, now "hi!"append changed the shared object
change's sb-> same StringBuilder-> new StringBuilder "new"reassigning changes only the copy
main's n11the int was copied
main's list-> ArrayList [a]-> same list, now [a, x]add changed the shared object
change's list-> same ArrayList-> new empty ArrayListreassigning changes only the copy
Mutating through a copied reference is visible; reassigning a parameter is not.

On entry, the method's sb points at the same StringBuilder as main's sb. So sb.append("!") changes that shared object, and main sees hi! afterwards. The next line creates a brand new StringBuilder and stores its reference in the parameter. Notice that this does not touch main's variable at all; it only changes where the local parameter points.

The int is the simplest case. The method received the number 1 as a copy, so setting n to 5 changes only that copy, and main still prints 1. The list follows exactly the same rule as the StringBuilder. Adding x goes through the shared reference, so it changes main's list. Reassigning list to a new ArrayList only redirects the parameter, so main still prints [a, x].

3Mutating Versus Reassigning

The whole puzzle reduces to one distinction. Calling a method on the object, such as append or add, follows the reference to the object and changes the object. Every variable that points at that object sees the change. Assigning a new value to the parameter changes the parameter variable, which is local to the method. The caller's variable is a different variable that was only copied from, so it keeps pointing where it always did.

If Java passed by reference, the method's parameter would be another name for main's variable itself, and reassigning sb inside change would make main print new. It prints hi!, which is the proof that only the reference value was copied. A useful way to say it is that Java passes references by value.

4Why a swap Method Cannot Work in Java

The same rule explains a classic exercise that has no solution in Java.

Java
static void swap(Integer a, Integer b) {
Integer tmp = a;
a = b; // changes only the local a
b = tmp; // changes only the local b
}
Integer x = 1, y = 2;
swap(x, y);
System.out.println(x + " " + y); // 1 2

Inside swap, a and b are copies. Swapping them swaps the copies, and the method returns without touching x and y. There is no way to write a method that reassigns the caller's local variables in Java, because the method never receives those variables, only their values. Notice also that Integer is immutable, so we cannot even change the objects themselves. With a mutable holder, such as an array of two elements or an AtomicReference, a method can swap what is inside the holder, but that is mutating a shared object again, not reassigning the caller's variables.

5Follow-up questions

How would you return two results from a method instead?

Return one object that carries both values. Since Java 16 the cleanest way is a small record, such as record MinMax(int min, int max), and the method returns new MinMax(lo, hi). The caller then reads result.min() and result.max(). This is clearer than filling an output parameter, because the method signature says exactly what comes back and the caller's variables are never changed behind its back. For a value that may be missing, Optional can be part of the record or the return type.

Are Strings an exception to the rule?

No. A String reference is copied like any other reference. Strings only look different because they are immutable: there is no method like append that changes a String in place. Code such as s = s + "!" inside a method creates a new String and reassigns the local parameter, so the caller never sees it. The rule is the same; the object just offers no way to mutate it.

Is it bad practice for a method to modify a list it receives?

It is not wrong, but it should be intended and visible. A method named addDefaults(list) that changes its argument is fine if its name and documentation say so. Surprise mutation is the problem: a method that sorts or trims a list the caller still uses elsewhere. Where a method only needs to read, accepting an unmodifiable view or copying the input makes the contract clear.

6Conclusion

In this answer, we traced a method that receives a StringBuilder, an int and a List. Java copied each argument's value: the number for the int, the reference for the objects. Changes made through the copied references, append and add, were visible to the caller, so it printed hi! and [a, x], while reassigning the parameters and setting n to 5 changed only local copies, so n stayed 1. The same rule explains why swap cannot work in Java and why returning a small record is the right way to produce two results.

What to say in the interviewAbout 70 seconds aloud

Open with

It prints hi!, then 1, then [a, x]. The rule behind it is that Java always passes arguments by value. For a primitive like int, the number is copied, so n = 5 inside the method changes only the local copy and the caller still has 1.

Then, if they want more

For objects, the value that gets copied is the reference, so the parameter and the caller's variable point to the same object. That means sb.append and list.add go through the reference and change the caller's StringBuilder and list, which is why we see the exclamation mark and the x.

But the next lines, sb = new StringBuilder and list = new ArrayList, only reassign the method's own parameter variables. They point the local copies at new objects that the caller never sees. If Java were pass by reference, the caller would print new and an empty list, and it does not.

The same reason means a swap method can never swap the caller's variables in Java. If I need two results from a method, I return a small record holding both instead of trying to change the caller's variables.

About this answer

Last updated
12 November 2026
Versions it names
Java 16
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What does this print, and why?": https://scrivio-interview-library.netlify.app/java/classes-and-objects/9925a241c6/