大哥们,帮我看看这个程序吧~~~~~~~~~

CTAer 2003-10-20 10:28:07
/**
本程序对断定进行测试
*/
public class Test11
{
public static void main(String[] args)
{
System.out.println("Testing ...");
int a1;
a1=-10;
if (a1>0)
{
}
else
{
assert (a1==0);
System.out.println("Hello");
}
}
}
以上是SL-275教程中的一个例子,我将a1赋值为-10,在assert(a1==0)处是否就应该结束执行,为什么还是输出"Hello"呢?
谢谢~
...全文
61 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
CTAer 2003-10-21
  • 打赏
  • 举报
回复
to tianmiaohu(Tianmiao) : 谢谢~
to whywzf(古风) : 不用if判断,用当assert后的逻辑表达式不成立时就会抛出异常
to computerbird(↘ぷ赶赶ぷ↙) : 可以编译通过,只要加上参数-source 1.4就可以了
fft123 2003-10-21
  • 打赏
  • 举报
回复
想结束程序的执行
在assert (a1==0);后加上break;
computerbird 2003-10-20
  • 打赏
  • 举报
回复
请问老兄:
在运行这个程序的时候会编译通过吗?
assert(a1==0)这里有问题吧!
whywzf 2003-10-20
  • 打赏
  • 举报
回复
不是,当a1<=0 时执行else
assert (a1==0);
System.out.println("Hello");
这两句顺序执行,都会执行,相当于false;System……;
不想输出的话用
if(assert(a1==0))
System.out.println("Hello");
tianmiaohu 2003-10-20
  • 打赏
  • 举报
回复
By default, assertions are disabled at runtime. Two command-line switches allow you to selectively enable or disable assertions

To enable assertions at various granularities, use the -enableassertions, or -ea, switch. To disable assertions at various granularities, use the -disableassertions, or -da, switch. You specify the granularity with the arguments that you provide to the switch:

no arguments
Enables or disables assertions in all classes except system classes.
packageName...
Enables or disables assertions in the named package and any subpackages.
...
Enables or disables assertions in the unnamed package in the current working directory.
className
Enables or disables assertions in the named class
For example, the following command runs a program, BatTutor, with assertions enabled in only package com.wombat.fruitbat and its subpackages:

java -ea:com.wombat.fruitbat... BatTutor
If a single command line contains multiple instances of these switches, they are processed in order before loading any classes. For example, the following command runs the BatTutor program with assertions enabled in package com.wombat.fruitbat but disabled in class com.wombat.fruitbat.Brickbat:

java -ea:com.wombat.fruitbat... -da:com.wombat.fruitbat.Brickbat BatTutor
The above switches apply to all class loaders. With one exception, they also apply to system classes (which do not have an explicit class loader). The exception concerns the switches with no arguments, which (as indicated above) do not apply to system classes. This behavior makes it easy to enable asserts in all classes except for system classes, which is commonly desirable.

To enable assertions in all system classes, use a different switch: -enablesystemassertions, or -esa. Similarly, to disable assertions in system classes, use -disablesystemassertions, or -dsa.

For example, the following command runs the BatTutor program with assertions enabled in system classes, as well as in the com.wombat.fruitbat package and its subpackages:

java -esa -ea:com.wombat.fruitbat...
The assertion status of a class (enabled or disabled) is set at the time it is initialized, and does not change. There is, however, one corner case that demands special treatment. It is possible, though generally not desirable, to execute methods or constructors prior to initialization. This can happen when a class hierarchy contains a circularity in its static initialization.

If an assert statement executes before its class is initialized, the execution must behave as if assertions were enabled in the class. This topic is discussed in detail in the assertions specification.

程序 = 数据结构 + 算法  程序是为了解决实际问题而存在的。然而为了解决问题,必定会使用到某些数据结构以及设计一个解决这种数据结构的算法。如果说各种编程语言是程序员的招式,那么数据结构和算法就相当于程序员的内功。编程实战算法,不是念PPT,我们讲的就是实战与代码实现与企业应用。程序 = 数据结构 + 算法                ——图灵奖得主,计算机科学家N.Wirth(沃斯)作为程序员,我们做机器学习也好,做python开发也好,java开发也好。有一种对所有程序员无一例外的刚需 —— 算法与数据结构日常增删改查 + 粘贴复制 + 搜索引擎可以实现很多东西。同样,这样也是没有任何竞争力的。我们只可以粘贴复制相似度极高的功能,稍复杂的逻辑没有任何办法。语言有很多,开发框架更是日新月异3个月不学就落后我们可以学习很多语言,很多框架,但招聘不会考你用5种语言10种框架实现同一个功能。真正让程序员有区分度,企业招聘万年不变的重点 —— 算法与数据结构。算法代表程序员水平的珠穆朗玛。 本视频由微软全球最有价值专家尹成录制,拒绝念PPT,代码实战数据结构与算法导论。除了传统数据结构算法,加入高并发线程安全数据结构,分布式负载均衡算法,分布式哈希表,分布式排序等等现代算法。  算法,晦涩难懂,却又是IT领域受重视的素养之一。可以说,算法能力往往决定了一个程序员能够走多远。因此,BAT/FLAG等国内外各大名企非常喜欢在面试环节考核求职者的算法编程,这也成为了无数准程序员们过不去的一道“坎”。如何入门并成为一名出色的算法工程师?但无论半路出家还是科班出身,除学生时代搞算法竞赛的同学外真正用心学习过算法与数据结构太少太少。对于后期想要学习算法与数据结构却不得不面对以下问题:没有自己的知识框架,无法关联知识点,学习效率低有疑问而无人解答,有问题无法理解全靠猜测,一个问题卡好几天市面上资料题解质量参差不齐,正确性未可知Google算法-工程师尹成大哥学习算法。

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧