4、带标号的语句:语句前面加上标识符(作为语句的标号)和冒号,break语句和continue语句可能会使用标号
例如:
rowLoop: for(int r = 0; r < rows.length; r++) { // A labeled loop
colLoop: for(int c = 0; c < columns.length; c++) { // Another one
break rowLoop; // Use a label
}
}
5、局部变量声明语句 [final] type name [= value] [, name [= value]] ...;
局部变量(简称变量),是存储值的位置的符号名,定义在方法中或复合语句中;所有变量在使用前必须先声明---这就是变量声明语句;
Java是强类型语言,变量声明规定了变量的类型,只有该类型的值可以存储在该变量中;
注:Pascal、C#是强类型语言,C、C++不是
1)最简单形式:只规定变量类型和变量名;比如
int counter;
2)在变量声明中包含初始化,即:等号后面的表达式规定了变量的初始值;例如:
int i = 0;
String s = readLine( );
int[] data = {x+1, x+2, x+3}; // Array initializers
3)单个变量声明语句可以声明并且初始化多个变量,条件是这些变量必须有相同的类型;变量之间用逗号隔开。例如:
int i, j, k;
float x = 1.0, y = 1.0;
String question = "Really Quit?", response;
4)Java1.1以后,变量声明语句可以用final关键字开头,表示:一旦变量的初始值被规定了,以后就不允许改变了
注:和C++的const相似
例如:
final String greeting = getLocalLanguageGreeting( );
5)局部变量声明可以出现在代码的任何地方,不一定非要在方法或代码块的开头;甚至可以出现在for循环的初始化部分;
6)局部变量只可以在定义它们的方法或代码块中使用,这称为该变量的作用域(scope)
10、for语句:for ( init ; test ; update ) statement
在for语句的init和update中,可以有用逗号分隔开的多个语句(这是Java中唯一可以用逗号分隔语句的地方!),这些语句顺序执行
11、for/in语句(见语句(二))
12、break语句:break [ label ] ;
1)简单的break:直接退出包含break的最内层的while、do、for、switch语句;
2)break后面带语句标号:立即退出被标记的块,这个块可以是任何类型的语句,不限于循环或switch。例如:
testfornull: if (data != null) { // If the array is defined,
for(int row = 0; row < numrows; row++) { // loop through one dimension,
for(int col = 0; col < numcols; col++) { // then loop through the other.
if (data[row][col] = = null) // If the array is missing data,
break testfornull; // treat the array as undefined.
}
}
} // Java interpreter goes here after executing break testfornull