代码改变世界

TIJ——Chapter Four:Controlling Execution

2014-12-14 23:40  星星之火✨🔥  阅读(211)  评论(0)    收藏  举报

同上一章,本章依然比较简单、基础,因此只是做一些总结性的笔记。

 

1、 不像C和C++那样(0是假,非零为真),Java不允许用一个数字作为boolean值。

2、 C中,为了给变量分配空间,所有变量的定义必须在代码块前面。而C++和Java中没有这个限制,它允许更加自然的代码

    风格,而且使得代码更容易理解。

3、 使用Foreach语法,我们不必创建一个用来遍历各个项的整型值,foreach自动的为我们提供每一个项。

4、 如果在返回值为void的方法中没有return语句,那么它隐式地在方法的结尾处返回,因此return语句并不总是必须的。然

    而,如果方法声明返回任何非void类型,我们必须保证代码的每个执行路径返回恰当的值。

5、 Java允许在循环体之前设置标签,并且循环体和标签之间不能有任何语句,它的主要作用是可以跳出多层循环,而break和

    continue只能打断当前的循环。

6、 The switch statement is a clean way to implement multiway selection(i.e., selecting from among a 

number of different execution paths), but it requires a selector that evaluates to an integral value, such 

as int or char. If you want to use, for example, a string or a floating point number as a selector, it won

work in a switch statement. For non-integral types, you must use a series of if statements. BTW, Java 

SE5s new enum feature helps ease this restriction, as enums are designed to work nicely with switch.