Java · 1 min · daily challenge of 2026-10-30
How Java compares string literals and objects
Learn how Java interns string literals, how new String creates distinct objects, and why equals compares characters while strings remain immutable.
The quiz
What does this print?
- true true true
- false false true
- true false true
- true false false
Show the answer
C. true false true
Transcript
This daily challenge question caught people out on Java string equality. Let us trace what actually prints.
Double equals checks if references point to the exact same object, returning false for separate instances, whereas equals compares characters and returns true.
Here is the key idea: string literals are interned to share one memory reference, while new String creates a separate object where equals matches.
Look at the trace: first, a double equals b gives true; second, a double equals c gives false; third, dot equals returns true on characters.
Remember strings are immutable in Java. Do not ignore the returned value after concat; you must reassign s to keep any new string.
Now pause right here, look at the code from this daily challenge question, and pick whether option A, B, C, or D prints.
The correct option is C, true false true, because using new String allocates a separate object while the characters remain completely equal.
Literals share memory while new objects do not. Save this summary to revise before your semester exams, and practice core concepts on InternDoor.