Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

What do these comparisons print?

Java
String a = "hello";
String b = "hel" + "lo";
String c = new String("hello");
String part = "hel";
String d = part + "lo";
System.out.println((a == b) + " " + (a == c) + " " + (a == d) + " " + (a == d.intern()));

The code prints "true false false true". a == b is true because "hel" + "lo" is a compile-time constant and becomes the same pooled literal as "hello"; a == c is false because new String always creates a new object; a == d is false because part is an ordinary variable, so the concatenation runs at run time and produces a new String; and a == d.intern() is true because intern() returns the pooled instance.

Background to this question

This is a classic warm-up question because == on strings is one of the most common bugs in junior code, and the reasons behind each line test whether we understand the string pool, compile-time constants and object identity. Getting all four right, and explaining the one-word change that flips the third result, shows we know what the compiler does and not just what the runtime does. In this answer, we will recap what == compares on objects and what the string pool is, go through the four comparisons one at a time, and then see how a final modifier changes the result.

1A Quick Recap: Identity Versus Equality

For objects, == compares references: it is true only when both sides point to the very same object in memory. equals() compares content: for String it is true when both have the same characters. So two different String objects with the text "hello" are equal by equals() but not by ==.

The string pool, a table the JVM keeps of shared String objects, is what makes == sometimes true for strings with the same text. Every string literal in our code, such as "hello", is pooled: when a class is loaded and the literal is first used, the JVM looks for an equal string in the pool and reuses it, so all occurrences of the literal "hello" in the whole program refer to one object.

2Running the Code

Here is the code with the result of each comparison as a comment.

Java
String a = "hello";
String b = "hel" + "lo";
String c = new String("hello");
String part = "hel";
String d = part + "lo";
System.out.println(a == b); // true
System.out.println(a == c); // false
System.out.println(a == d); // false
System.out.println(a == d.intern()); // true
System.out.println(a.equals(d)); // true: same characters
// the original single println prints: true false false true
Why each comparison gives its result
ExpressionResultReason
a == btrue"hel" + "lo" is folded by javac into the literal "hello"
a == cfalsenew String always allocates a new object
a == dfalsepart is not a constant, so + runs at run time
a == d.intern()trueintern() returns the pooled "hello"
Only compile-time constants and interned strings share the pooled object.

Now let us see why each line gives its result.

3Why a == b Is True

The expression "hel" + "lo" is a constant expression: it is built only from literals, so its value is known when the code is compiled. The Java Language Specification requires the compiler to evaluate such expressions itself, so javac writes the single literal "hello" into the class file. At run time there is no concatenation at all.

So the line String b = "hel" + "lo" compiles to exactly the same bytecode as String b = "hello". Both a and b load the same pooled literal, and == is true. We can confirm this by running javap -c on the class: both lines show ldc "hello" and no concatenation call.

4Why a == c and a == d Are False

For c, the reason is simple: new always creates a new object. new String("hello") copies the pooled literal into a fresh String, so c has the same characters as a but is a different object.

For d, the key detail is that part is an ordinary local variable. Even though it was assigned a literal, the compiler does not treat its value as a constant, so part + "lo" is compiled as a real concatenation, done at run time with invokedynamic since JDK 9. A runtime concatenation always produces a new String object that is not in the pool, so a == d is false even though a.equals(d) is true.

5Why a == d.intern() Is True

intern() looks up the pool for a string equal to d. One is already there: the literal "hello" that a points to. So intern() returns that pooled object, not d itself, and the comparison with a is true. If no equal string had been in the pool, intern() would have added d and returned d.

Notice that intern() does not change d. It returns a reference; d still points to its own runtime-created object. That is why we always use the return value, as in d = d.intern(), when we want the canonical instance.

6Making a == d True With final

The follow-up interviewers like is the one-word change. If we declare part as final and initialise it with a constant, it becomes a constant variable, which the specification treats like a literal inside constant expressions.

Java
final String part = "hel"; // a constant variable: final and initialised with a constant
String d = part + "lo"; // now a constant expression, folded to "hello" by javac
System.out.println(a == d); // true
final String later; // final, but not initialised with a constant in its declaration
later = "hel";
String e = later + "lo";
System.out.println(a == e); // false: not a constant variable, so concatenated at run time

The rule is precise: the variable must be final, of a primitive type or String, and initialised in its declaration with a constant expression. Then the compiler can substitute its value and fold the whole expression. The second example shows that final alone is not enough. This is also a nice reminder of the practical lesson: the outcome of == on strings depends on compiler details, so in real code we always compare strings with equals().

7Follow-up questions

Does this behave the same in jshell?

Not always. jshell turns top-level variables into fields of a generated class, and it does not keep the final modifier on top-level declarations, so the final example can print false there. Inside a method in a normal class, it prints true as described.

Are strings created by concatenation ever pooled automatically?

No. Only literals and constant expressions are pooled when classes load, and runtime strings enter the pool only when we call intern(). String deduplication in the garbage collector can make equal strings share their internal byte array, but it never changes identity, so == stays false.

Why not just use == when we know both sides are literals?

Because it is fragile. A later change, such as reading the value from configuration, a method that builds it, or a refactoring that removes final, silently turns true into false, and nothing fails at compile time. equals() works in every case and costs almost nothing when the references are already the same, because String.equals checks identity first.

8Conclusion

In this answer, we saw that == compares object identity and that the string pool shares literal objects. "hel" + "lo" is folded at compile time into the literal "hello", so a == b is true; new String always makes a new object; part + "lo" runs at run time because part is not a constant variable, so it creates a new object; and intern() returns the pooled instance, so a == d.intern() is true. Declaring part as final with a constant initialiser makes it a constant variable and flips a == d to true, which is a good reason to always compare strings with equals().

What to say in the interviewAbout 80 seconds aloud

Open with

It prints true, false, false, true. The first one is true because "hel" plus "lo" is a compile-time constant expression, so javac folds it into the literal "hello", and all occurrences of a literal share one pooled object.

Then, if they want more

The second is false because new String always creates a new object, even though it has the same characters.

The third is false because part is an ordinary local variable, not a constant variable, so part plus "lo" is a real concatenation done at run time, and that always gives a fresh String that is not in the pool. a.equals(d) would still be true.

The fourth is true because intern looks up the pool, finds the literal "hello" that a already points to, and returns that object. One detail I would add is the final variant.

If part is declared final and initialised with a literal, it becomes a constant variable, the compiler can substitute its value, and part plus "lo" is folded at compile time, so a == d becomes true. That fragility is exactly why in real code I always compare strings with equals and never with double equals.

About this answer

Last updated
12 November 2026
Versions it names
Java 9
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 do these comparisons print?": https://scrivio-interview-library.netlify.app/java/strings/7dc426432d/