Scrivio Interview library
CodingFoundations5 min read · updated 12 November 2026

What does this print and why?

Java
String s = "java";
s.toUpperCase();
s.concat("21");
System.out.println(s);

It prints java. toUpperCase and concat do not change s, because String is immutable; each of them builds and returns a new String object instead. The code calls both methods but never assigns their results, so the new strings JAVA and java21 are created and immediately thrown away, and s still refers to the original java. Static analysis tools flag ignored return values of String methods for exactly this reason.

Why this question is asked

This is one of the most common bugs in code written by people who are new to Java, and it still slips into experienced codebases, especially when someone is used to a mutable string type from another language or to builder-style APIs. Interviewers ask it because it checks whether we really understand what immutability means in practice, not just as a definition. In this answer, we will recap how String methods behave, trace the snippet line by line, fix it, look at a few look-alike bugs, and see how tools catch them before review.

1A Quick Recap: String Methods Return New Strings

A String in Java is immutable: once created, its characters can never change. So every method that seems to modify a string, such as toUpperCase, toLowerCase, trim, strip, replace, substring and concat, works the same way. It reads the original characters, builds a new String with the result, and returns it. The original object is left exactly as it was.

The consequence is simple but easy to forget: the result of these methods exists only in the return value. If we do not assign it to a variable or pass it somewhere, it is lost.

2Tracing the Snippet Line by Line

Let us follow what happens to each object.

Java
String s = "java"; // s refers to the String "java"
s.toUpperCase(); // creates "JAVA", returns it; nobody keeps it
s.concat("21"); // creates "java21", returns it; nobody keeps it
System.out.println(s); // java
Step throughWhat happens to the results
main"java" (s)new Strings
toUpperCase()
builds "JAVA"
returns "JAVA", ignored
concat("21")
builds "java21"
returns "java21", ignored
println(s) reads "java"

7 messages in order. Play them, or step through them one at a time.

The original object is never changed; the new ones are created and dropped.

The first line makes s refer to the literal java. The second line calls toUpperCase on that object. The method creates a brand new String containing JAVA and returns it, but the statement does nothing with the return value, so the new string becomes garbage straight away. The third line does the same with concat: a new String java21 is created and discarded. Notice that s was never reassigned, and the object it refers to cannot change, so the last line prints java.

3Fixing It

To keep a result, we assign it. We can reassign s itself, which makes the variable point at the new object, or use a new variable, which is often clearer because each name then means one thing. We can also chain the calls, because each method returns a String that the next method can be called on.

Java
String s = "java";
s = s.toUpperCase(); // s now refers to "JAVA"
s = s.concat("21"); // s now refers to "JAVA21"
System.out.println(s); // JAVA21
String name = "java";
String label = name.toUpperCase().concat("21"); // chained
System.out.println(name + " " + label); // java JAVA21

Notice that in the first version, the object java is still unchanged. What changed is the variable s, which now refers to a different object. Variables can be reassigned; String objects cannot be modified. Keeping those two ideas apart is the whole point of the question.

4Look-Alike Bugs

The same mistake appears with every String method that returns a new value, and with other immutable types such as BigDecimal and the java.time classes, whose methods also return new objects. These lines all compile and all do nothing useful.

Java
input.trim(); // the trimmed copy is lost
path.replace('\\', '/'); // the result is lost, path still has backslashes
BigDecimal total = BigDecimal.ZERO;
total.add(price); // total is still 0
LocalDate due = LocalDate.of(2026, 1, 31);
due.plusDays(30); // due is still 2026-01-31

The fix in each case is the same: assign the result, for example total = total.add(price). StringBuilder is the exception that proves the rule: sb.append(x) does change the builder, because StringBuilder is mutable, which is why ignoring its return value is fine.

5How Tools Catch It

Because the code compiles and the bug only shows up as wrong output, it is worth letting tools find it. IntelliJ IDEA warns that the result of a String method call is ignored. SpotBugs reports a return value ignored pattern for methods like these (RV_RETURN_VALUE_IGNORED), and Error Prone has a ReturnValueIgnored check that covers String, BigDecimal and similar immutable types, plus a CheckReturnValue annotation we can put on our own methods. Turning on one of these in the shared build catches the bug in every module.

6Follow-up questions

Why does Java allow calling a method and ignoring its result at all?

Because many methods are called for their side effects and also return something we often do not need, such as List.add returning a boolean. The compiler cannot tell which results matter, so the language allows ignoring any of them and leaves it to static analysis with annotations like CheckReturnValue.

Does s.concat("21") create a new object even when the argument is empty?

No. concat returns the same object when the argument is empty, because nothing would change. Likewise, toUpperCase may return the original object when it is already upper case, as current JDKs usually do. These are optimisations, and code must never depend on them.

Would s += "21" have worked?

Yes, because += is an assignment: s += "21" means s = s + "21", which builds a new String and makes s refer to it. That is why the compound form works while the plain call to concat does not.

7Conclusion

In this answer, we saw that the snippet prints java because String is immutable: toUpperCase and concat return new String objects and never change the original, and since their results are not assigned, they are discarded immediately. We fixed it by assigning or chaining the results, saw the same bug with trim, replace, BigDecimal and java.time, and saw that IDE inspections, SpotBugs and Error Prone flag ignored return values.

What to say in the interviewAbout 80 seconds aloud

Open with

It prints java. String is immutable, so toUpperCase and concat never change the object s refers to. Each of them builds a new String, JAVA and java21 here, and returns it.

Then, if they want more

The code calls both but does nothing with the return values, so those new strings are created and immediately become garbage, and s still refers to the original java when it is printed.

To fix it I would assign the result, either s = s.toUpperCase() and then s = s.concat("21"), or in one line with chaining, s = s.toUpperCase().concat("21"), which gives JAVA21.

The distinction I would make clear is between the variable and the object: the variable s can be reassigned to point at a new String, but the String object itself can never be modified. The same bug happens with trim, replace and substring, and with other immutable types like BigDecimal and LocalDate, where total.add(price) on its own leaves total unchanged.

StringBuilder is different because it is mutable, so append really does change it. Since this compiles and just gives wrong output, I would rely on static analysis, IntelliJ's ignored result inspection, SpotBugs' return value ignored check or Error Prone's ReturnValueIgnored, to catch it in the build.

About this answer

Last updated
12 November 2026
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/strings/3107eed455/