Machine-translated by Google. Questions are authored in English.
Top

Discussion

Which line contains the error?

public class Sample82
{
    public static void main(String[] args)
    {
        String s = null;
        assert s != null & s.length() > 0;
        System.out.println("ok");
    }
}

  • A.No line here is in error.
  • B.assert s != null & s.length() > 0;
  • C.String s = null;
  • D.System.out.println("ok");

Answer: B

The single ampersand is the non-short-circuiting operator, so both sides are evaluated and s.length() throws NullPointerException despite the guard to its left. Written with && the second operand would be skipped and the assertion would fail cleanly instead.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.