`

Java基础恶补——控制流、异常、断言

    博客分类:
  • Java
 
阅读更多

[SCJP Sun Certified Programmer for Java 6 Study Guide (Exam 310-065)]  chapter5

 

一. if switch
1. if 的 statements 只能使用布尔表达式(小心区分 == 和 =)。
2. if 块的 {} 是可选的,但推荐使用(即使只有1行语句)以增强可读性。

3. switch 的 statements 只能使用 enums,  byte, short, int, char.

4. case 常量必须是 literal 或 final 变量 或1个常量表达式(包括 enum),不能用非 final 变量 和1个值的范围。

5. switch case 语句的执行从第1个匹配的 case 块开始,直到遇到 break 则终止执行。case 的作用只是相当于1个入口,而不仅仅只执行这个 case 中的语句。

6. The default keyword should be used in a switch statement if you want to run some code when none of the case values match the conditional value.

7. default 块可以写在 switch 语句的任何位置,当没有 case 匹配时,default 块将执行,如果没有 break,default 块后面的语句都将被执行。

 

二. 循环

1. 基本的循环分3个部分:声明和/或初始化、布尔判断、循环体。

2. If a variable is incremented or evaluated within a basic for loop, it must be declared before the loop, or within the for loop declaration.

3. A variable declared (not just initialized) within the basic for loop declaration cannot be accessed outside the for loop (in other words, code below the for loop won't be able to use the variable).

4. for循环的声明中可以在第1部分初始化多个同类型的变量,变量之间用分号分隔。

5. 增强型的for循环(从Java 6开始)有2个部分:声明、表达式,通常用于数组或集合的循环。

6. With an enhanced for, the expression is the array or collection through which you want to loop.

7. With an enhanced for, the declaration is the block variable, whose type is compatible with the elements of the array or collection, and that variable contains the value of the element for the given iteration.

8. 条件判断表达式不能只有1个数字型变量,if (x) 的这种写法,仅当x是布尔类型时才是合法的。

9. do循环的循环体至少会执行1次,即使if条件不满足也是如此。

 

三. break 和 continue

1. 1个 unlabeled break 的结果是:终止当前循环,从紧跟着这个的循环的语句开始继续执行。

2. 1个 unlabeled continue 的结果是:终止当前循环的这一轮,继续执行这个的循环的下一轮。

3. 如果 break continue 是 labeled, 则与前面的区别是:终止的不是当前的循环,而是被标记的循环。

 

四. 异常处理

1. 异常有2种:checked 和 unchecked。

2. Checked exceptions 包括所有 Exception 的子类,不包括 RuntimeException 的子类。

3. Checked exceptions 主要用于处理或声明规则,任何方法可以抛出 a checked exception ,checked exception要么被抛出、要么被处理。

4. Error 或 RuntimeException 是 unchecked ,所以编译器不能强迫处理或声明它们。

5. If you use an optional finally block, it will always be invoked, regardless of whether an exception in the corresponding try is thrown or not, and regardless of whether a thrown exception is caught or not.

6. finally 块不会被执行的唯一例外是JVM停止了,比如 try 或 catch 中调用了 System.exit().

7. finally 被调用并不意味着它会完成,比如 finally 块中调用了 System.exit() 或 出现异常。

8. Uncaught exceptions propagate back through the call stack, starting from the method where the exception is thrown and ending with either the first method that has a corresponding catch for that exception type or a JVM shutdown (which happens if the exception gets to main(), and main() is "ducking" the exception by declaring it).

9. 可以自定义异常,继承 Exception 或它的子类即可,这个自定义异常将被作为 checked exception。

10. catch 块是有顺序的,子类型的异常必须写在前面,否则这个异常将被父类异常的 catch 块所捕捉,从而失去了其子类特性,编译器在1个异常被捕捉后就不会执行其他 catch 块了。

11. 一些异常是由程序员创建的,一些是由JVM创建的。

 

五. 断言

1. 断言提供了一种在开发和调试过程中测试你的设想的方法。

2. 断言通常用于测试阶段,部署阶段一般会被禁止。

3. Java1.4时,可以把 assert 作为关键字或标识符,只能选择其一,不能同时使用。 要把 assert 作为标识符使用时,编译时加上 -source 1.3 参数。

4. 运行时,断言默认是禁用的。若要启用,则在命令行加上 -ea 或 -enableassertions, 用 -da 或  -disableassertions 则表示禁用.

5. 不带参数 -ea 或 -da 表示对所有类有效,也可以只对某些包、某些类单独设置启用或禁用断言。

6. 不适合使用断言的情况:

1) 验证参数。

2) 断言表达式会影响效果时;断言是表示不希望遇到的情况,不能依赖断言来控制行为。

7. 适合使用断言的情况:验证一个从不会被到达的代码块。可以使用 assert false; 当执行到时会引发1个断言异常。

 

断言启用、禁用命令行举例:

Command-Line Example What It Means
java -ea
java -enableassertions
Enable assertions.
java -da
java -disableassertions
Disable assertions (the default behavior of Java 6).
java -ea:com.foo.Bar Enable assertions in class com.foo.Bar.
java -ea:com.foo... Enable assertions in package com.foo and any of its subpackages.
java -ea -dsa Enable assertions in general, but disable assertions in system classes.
java -ea -da:com.foo... Enable assertions in general, but disable assertions in package
com.foo and any of its subpackages.

 

附:

一篇不错的讲解Java异常的文章

java常见异常及一般原因

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics