Question 65
Given:
12. public class AssertStuff {
13.
14. public static void main(String [] args) {
15. int x= 5;
16. int y= 7;
17.
18. assert (x> y): “stuff”;
19. System.out.println(”passed”);
20. }
21. }
And these command line invocations:
java AssertStuff
java -ea AssertStuff
What is the result?
A. passed
stuff
B. stuff
passed
C. passed
An AssertionError is thrown with the word “stuff” added to the stack
trace.
D. passed
An AssertionError is thrown without the word “stuff” added to the
stack trace.
E. passed
An AssertionException is thrown with the word “stuff” added to the
stack trace.
F. passed
An AssertionException is thrown without the word “stuff” added to the
stack trace.Answer: C我的答案是,出现:
Exception in thread "main" java.lang.AssertionError: just do it!!
        at wren4255.main(wren4255.java:51)

解决方案 »

  1.   

    你jdk的版本是 1.4以上的吗?
      

  2.   

    assert 断言还没学。 Mark 学习。
      

  3.   


    是哦。我的答案是,出现: 
    Exception in thread "main" java.lang.AssertionError: stuff
            at wren4255.main(wren4255.java:51)我感觉这道题不应该输出passed,因为程序到assert (x> y): “stuff”;就结束了。
      

  4.   

    这是assert的一种用法:
       assert exp1:exp2
    当程序运动到assert(x>y):"stuff"的时候,exp1为false,后面的表达式的结果会被计算出来并作为AssertionError得构造器参数,而当exp1为true,会忽略exp2;
    但这些都不影响最后System.out.println("passed")的输出!
      

  5.   

    另外:
    1,当java AssertStuff的时候,assert那条语句是忽略的,所以输出passed;2,而当java -ea AssertStuff,assert语句是起作用的,由于exp1为false,所以就输出An AssertionError is    thrown with the word “stuff” added to the stack 
    trace;如果你把x>y改成x<y,则会输出passed!