紫米黑米 2014-09-07 09:15 采纳率: 0%
浏览 1825

一道完全不理解是什么意思的题目

Given:
11. public void go(int x) {
12. assert (x > 0);
13. switch(x) {
14. case 2: ;
15. default: assert false;
16. }
17. }
18. private void go2(int x) { assert (x < 0); }
Which statement is true?
A. All of the assert statements are used appropriately.
B. Only the assert statement on line 12 is used appropriately.
C. Only the assert statement on line 15 is used appropriately.
D. Only the assert statement on line 18 is used appropriately.
E. Only the assert statements on lines 12 and 15 are used appropriately.
F. Only the assert statements on lines 12 and 18 are used appropriately.
G. Only the assert statements on lines 15 and 18 are used appropriately
assert()这个函数的参数是布尔值吗?最后那个go2()是什么意思?为什么assert()的声明要在15—18行?感觉应该在17行之后吧,因为方法中不可以写方法呀。

  • 写回答

1条回答 默认 最新

  • lotus_waterlily 2014-09-09 02:47
    关注

    J2SE 1.4在语言上提供了一个新特性,就是assertion功能,它是该版本在Java语言方面最大的革新。

    从理论上来说,通过 assertion方式可以证明程序的正确性,但是这是一项相当复杂的工作,目前还没有太多的实践意义。

    在实现中,assertion就是在程序中的一条语句,它对一个boolean表达式进行检查,一个正确程序必须保证这个boolean表达式的值为true;如果该值为false,说明程序已经处于不正确的状态下,系统将给出警告或退出。

    一般来说,assertion用于保证程序最基本、关键的正确性。assertion检查通常在开发和测试时开启。为了提高性能,在软件发布后,assertion检查通常是关闭的。

    1、语法表示

    在语法上,为了支持assertion,Java增加了一个关键字assert。它包括两种表达式,分别如下:
    assert expression1;
    assert expression1:expression2;

    在两种表达式中,expression1表示一个boolean表达式, expression2表示一个基本类型或者是一个对象(Object),基本类型包括boolean,char,double,float,int和 long。由于所有类都为Object的子类,因此这个参数可以用于所有对象。

    2、含义

    在运行时,如果关闭了assertion功能,这些语句将不起任何作用。如果打开了assertion功能,那么expression1的值将被计算,如果它的值为false,该语句强抛出一个AssertionError对象。

    如果assertion语句包括expression2参数,程序将计算出 expression2的结果,然后将这个结果作为AssertionError的构造函数的参数,来创建AssertionError对象,并抛出该对 象;如果expression1值为true,expression2将不被计算。

    一种特殊情况是,如果在计算表达式时,表达式本身抛出Exception,那么assert将停止运行,而抛出这个Exception。

    评论

报告相同问题?

悬赏问题

  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常