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

Discussion

Which line will cause a compilation error?

public class Sample08
{
    static boolean ready = true;

    static void check()
    {
        assert ready;
    }

    public static void main(String[] args)
    {
        assert check();
    }
}

  • A.assert ready;
  • B.static boolean ready = true;
  • C.The program compiles without error.
  • D.assert check();

Answer: D

check() is declared void, so it yields no value and cannot serve as the boolean condition of an assert. The assertion inside check() is fine. Either give check() a boolean return type or call it as an ordinary statement and assert on something else.

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

Post your comment

Please log in or register to post a comment.