Boolean Expressions

AP Computer Science A· difficulty 3/5

To safely compare a string with a literal that might be null, use

  • A

    s.equals("yes")

  • B

    s == "yes"

  • C

    "yes".equals(s) (avoids NPE if s is null)

    check_circle
  • D

    Both A and C work

Explanation

Calling <code>.equals</code> on the literal is a defensive idiom because the literal is never null.

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

Related questions