String Objects: Concatenation, Literals, and More

AP Computer Science A· difficulty 4/5

What does this output?

<code>String a = "hi"; String b = "hi"; String c = new String("hi"); System.out.println(a == b); System.out.println(a == c); </code></pre>

  • A

    false / true

  • B

    true / true

  • C

    false / false

  • D

    true / false

    check_circle

Explanation

String literals are interned: a and b point to same pool object → <code>==</code> true. <code>new String(...)</code> allocates a new object → <code>==</code> false. Use <code>.equals()</code> for content comparison.

Want 10 more like this — adaptive to your weak spots?

Related questions