AI InterviewSearch…Search problems, topics, or pages…Ctrl KGet started
Mediumjavastringsimmutability

~105s

String against StringBuilder as a parameter

What does this Java program print?

public class Main {
    static void change(String s, StringBuilder sb) {
        s = s + " changed";
        sb.append(" changed");
    }

    public static void main(String[] args) {
        String s = "A";
        StringBuilder sb = new StringBuilder("B");
        change(s, sb);
        System.out.println(s + " " + sb);
    }
}
Show the answer and the worked solution

Correct answer: B.

A B changed

Worked solution

  1. s = s + " changed" builds a new String and points the local parameter at it, leaving the caller's s untouched at A.
  2. sb.append modifies the StringBuilder object itself, and the caller holds a reference to that same object, so the change is visible.
  3. The program prints A B changed.

Answer: A B changed.

The idea

Mutating the object a reference points at is visible to the caller; repointing the reference is not. Because String is immutable, only the second kind of operation is ever available for it.

Shortcut. Ask which line mutates an object: append does, while s = s + … only repoints the local name, so only sb's text changes.

Common trap. "A changed B changed" treats s = s + " changed" as changing the caller's String, when it builds a new String bound only to the parameter.

String against StringBuilder as a parameter — Aptitude

String against StringBuilder as a parameter: What does this Java program print? Answer and worked solution on CodeKairo.

Question

What does this Java program print?

public class Main {
    static void change(String s, StringBuilder sb) {
        s = s + " changed";
        sb.append(" changed");
    }

    public static void main(String[] args) {
        String s = "A";
        StringBuilder sb = new StringBuilder("B");
        change(s, sb);
        System.out.println(s + " " + sb);
    }
}

Options

  1. A B
  2. A B changed
  3. A changed B
  4. A changed B changed
Show the answer and the worked solution

Correct answer: B. A B changed

Worked solution

  1. s = s + " changed" builds a new String and points the local parameter at it, leaving the caller's s untouched at A.
  2. sb.append modifies the StringBuilder object itself, and the caller holds a reference to that same object, so the change is visible.
  3. The program prints A B changed.

Answer: A B changed.

The idea

Mutating the object a reference points at is visible to the caller; repointing the reference is not. Because String is immutable, only the second kind of operation is ever available for it.

Shortcut. Ask which line mutates an object: append does, while s = s + … only repoints the local name, so only sb's text changes.

Common trap. "A changed B changed" treats s = s + " changed" as changing the caller's String, when it builds a new String bound only to the parameter.

All Programming Fundamentals questions · placement mock tests