InternDoor

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.

Practise JavaMore videos

The quiz

What does this print?

  1. true true true
  2. false false true
  3. true false true
  4. 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.

More study videos

All videos
Learned it? See who is hiring

Engineering internships and entry-level jobs in India, newest first.