390 likes | 555 Views
一则广告. 急招四名有经验的兼职 JSP 程序员 国庆需要加班; 做 JSP 界面工作(不包括美工和 serverlet); 最好是一个互相比较熟悉,有一定配合的团队,有一个项目领导最好; 联系人:我, libo@buaa.edu.cn. 第三章 表达式与流控制. 运算符与表达式 流控制. 运算符与表达式. 与 C 不同之处: Java 是强类型语言,类型限制比 C 严格,运算 符的操作对象的类型会受到更多限制。 Java 不提供指针运算符,增加了对象操作符 instanceof , 字符串运算符 + 和零填充的右移 >>> 等。.
E N D
一则广告 • 急招四名有经验的兼职JSP 程序员 • 国庆需要加班; • 做JSP 界面工作(不包括美工和serverlet); • 最好是一个互相比较熟悉,有一定配合的团队,有一个项目领导最好; • 联系人:我,libo@buaa.edu.cn
第三章 表达式与流控制 • 运算符与表达式 • 流控制
运算符与表达式 • 与C不同之处: • Java是强类型语言,类型限制比C严格,运算 • 符的操作对象的类型会受到更多限制。 • Java不提供指针运算符,增加了对象操作符 • instanceof,字符串运算符+和零填充的右移>>>等。
Java操作符类别 • 算术运算操作符:+,-,*,/, %, ++, - - • 关系操作符:>, >=, < <=, ==, != • 位操作符:>>, <<, >>>, & , |, ^(逐位异或),~ (按位取反) • 逻辑操作符:&, |, !, ^(异或),&&, || • 赋值操作符:=, +=, -=, *=, /=, %=,&=, |=, ^=, <<=, >>=, >>>= • 其它操作符: ?: , [], . , ( ),(type), new, instanceof
位操作示例 public class BitwiseDemo { static final int VISIBLE = 1; static final int DRAGGABLE = 2; static final int SELECTABLE = 4; static final int EDITABLE = 8; public static void main(String[] args) { int flags = 0; flags = flags | VISIBLE; flags = flags | DRAGGABLE; if ((flags & VISIBLE) == VISIBLE) { if ((flags & DRAGGABLE) == DRAGGABLE) { System.out.println("Flags are Visible and Draggable."); } }
flags = flags | EDITABLE; if ((flags & EDITABLE) == EDITABLE) { System.out.println("Flags are now also Editable."); } } } 运行结果: Flags are Visible and Draggable. Flags are now also Editable.
逻辑操作符与位操作符 • 逻辑操作符与位操作符相同的,根据操作数判定是何种运算符 • &, | -- 称为不短路与、或; • &&,|| -- 称为短路与、或。
用+ 运算符连接字符串 • String salutation = “Dr. ”; • String name = “Pete” + “Seymour” ; • String title = salutation + name ; • 则title 值:Dr. Pete Seymour
右移操作符 >> 和 >>> >> :带符号右移 -256 >> 4 = -256/24 = -16 1010 … >> 2 111010... >>>:无符号右移,以0 填充 1010 … >>> 2 001010 ...
Java 强制类型转换 一般形式:(type) expression 例:(float)x/2 对强制类型转换的限制: 整型与浮点型可以相互转换,但基本类型 和数组、对象等复合类型之间不能互相转换。
流控制 while do … while for If … else switch
while 语句 一般格式: while (expression) { statement(s) }
While语句举例 public class WhileDemo { public static void main(String[] args) { String copyFromMe = "Copy this string until you " + "encounter the letter 'g'."; StringBuffer copyToMe = new StringBuffer(); int i = 0; char c = copyFromMe.charAt(i); while (c != 'g') { copyToMe.append(c); c = copyFromMe.charAt(++i); } System.out.println(copyToMe); } } 结果: Copy this strin
do-while语句 一般格式: do { statement(s) } while (expression);
do-while语句示例 public class DoWhileDemo { public static void main(String[] args) { String copyFromMe = "Copy this string until you " + "encounter the letter 'g'."; StringBuffer copyToMe = new StringBuffer(); int i = 0; char c = copyFromMe.charAt(i); do { copyToMe.append(c); c = copyFromMe.charAt(++i); } while (c != 'g'); System.out.println(copyToMe); } } 结果: Copy this strin
For 语句 一般格式: for (initialization; termination; increment) { statement(s) }
For 语句示例 public class ForDemo { public static void main(String[] args) { int[] arrayOfInts = { 32, 87, 3, 589, 12, 1076, 2000, 8, 622, 127 }; for (int i = 0; i < arrayOfInts.length; i++) { System.out.print(arrayOfInts[i] + " "); } System.out.println(); } } 运行结果: 32 87 3 589 12 1076 2000 8 622 127
If … else 语句 一般格式: if (expression) { statement(s1) } else{ statement(s2) }
If … else 语句示例 public class IfElseDemo { public static void main(String[] args) { int testscore = 76; char grade; if (testscore >= 90) { grade = 'A'; } else if (testscore >= 80) { grade = 'B'; } else if (testscore >= 70) { grade = 'C'; } else if (testscore >= 60) { grade = 'D'; } else { grade = 'F'; } System.out.println("Grade = " + grade); } } 运行结果: Grade = C
Switch语句 switch (expr1){ case expr2: … ; break; case expr3: … ; break; … } 注意:expr1的值必须是与int兼容的类型,byte, short,char而浮点型(float, double)或long 不可以。
Switch 与 if 语句 int month = 8; if (month == 1) { System.out.println("January"); } else if (month == 2) { System.out.println("February"); } . . . // and so on If 语句可以使用一个值的范围或条件进行判断, 而switch只能根据单个整型值进行判断, 并且这个值必须唯一。
Break语句 Break语句结束了switch 语句。程序流控制转到 switch 语句块后的第一条语句。没有break语句,程序控制将执行随后的各个 case 语句。
Break语句示例 public class SwitchDemo2 { public static void main(String[] args) { int month = 2; int year = 2000; int numDays = 0; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: numDays = 31; break;
case 4: case 6: case 9: case 11: numDays = 30; break; case 2: if ( ((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0) ) numDays = 29; else numDays = 28; break; } System.out.println("Number of Days = " + numDays); } } 运行结果: Number of Days = 29
Default 语句 在 switch 语句块的最后,对于没有匹配任何case语句条件的值,执行 default 语句。
特殊跳转语句 break [label] 从switch语句、循环语句中跳出。 continue[label] 跳过标号循环体的其余部分,不带label 跳过 最内层循环的剩余语句。 label: statement
特殊跳转语句举例 1Loop: while (true){ 2 for( … ){ 3 switch( ){ 4 case -1: 5 case ‘\n’: 6break loop ; 7 … 8 } 9 } 10 } 11 test: for( … ){ 12 … 13 while(… ){ 14 if( …){ 15 … 16continue test ; 17 } 18 } 19 } //跳出while去11行 // 跳到11行
关于程序控制的小经验 • 永远不要使用goto 或者标号,这都是旧的语法,虽然可能不会伤害可读性,但是仍然不是好的编码习惯; • 能够使用for 循环的时候,最好用for 循环,效率最高,代码最简单; • 多层嵌套的if…else 很难读,要注意格式和注释