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

Discussion

Which line is an inappropriate use of an assertion?

public class Sample27
{
    public boolean equals(Object o)
    {
        assert o != null;
        return o instanceof Sample27;
    }
}

  • A.public boolean equals(Object o)
  • B.Every line above is correct as written.
  • C.assert o != null;
  • D.return o instanceof Sample27;

Answer: C

The equals contract requires that x.equals(null) return false rather than fail, so a null argument is expected input and not a broken invariant. With -ea this assertion turns a contract-conforming call into an AssertionError. The instanceof test already handles null correctly by returning false.

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

Post your comment

Please log in or register to post a comment.