java assert的問題

public class test{

public static void main(String args[]){

int x=0;

assert(x>0):"assertion failed";

System.out.println("finished");

}

}

what is the result?

a.finished

b.compilation fails

c.An AssertionError is thrown

d.An AssertionError is thrown and finished is output

為什答案是a

assert遇到false會執行不是嗎?

Update:

這是testking scjp的模擬試題這種題目一般都是假設有下

-ea指令啦!!哇回答的都不一樣咧~能有高手在做出正確

的答案嗎~~感謝哦!!

3 Answers

Rating
  • Anonymous
    2 decades ago
    Favorite Answer

    這一題答案錯了!

    正解是 C

    你說的沒錯,assert遇到false就會丟出AssertionError!!

  • ?
    Lv 6
    2 decades ago

    Assert預設是不會執行的,除非執行時您下-ea的指令。。。

    http://caterpillar.onlyfun.net/Gossip/JavaGossip-V...

    2005-11-09 22:09:29 補充:

    您為什麼不自己編譯、執行看看呢?一試就知道結果的程式。。。XD

    2005-11-09 22:10:21 補充:

    沒下-ea是(A),有下-ea是(C)。。。

  • Anonymous
    2 decades ago

    assert遇到false 並不會執行阿!

Still have questions? Get your answers by asking now.