Reading "Thinking in Java" #2

本文深入探讨Java中的for循环特性,包括初始化定义多个变量、逗号运算符的使用,以及与while(true)的对比。同时,文章详细介绍了switch语句的高效应用技巧,通过案例展示如何利用其特性实现简洁的代码逻辑。
For
那么,这次来复习 "for"。  首先是形式:
for(initialization; Boolean-expression; step)
  statement

这个是再熟悉不过了,对于我来说自从初中开始学习PASCAL开始for循环就一直是使用率最高的循环控制结构。然而,通常我只是使用单个变量最为参数的for循环,其实for循环的initialization 定义多个同型的变量。例如:
( i = 0, j = 1; i < 10 && j != 11; i++, j++)
  

注意此处逗号(comma)的特殊用法,在for循环中的 initializationstep 部分是Java中唯一如此使用逗号的地方。在这里,Bruce Eckel称这时的 comma 为the comma operator 。而 initializationstep 中被逗号分割的statement将按照顺序方式执行,仿佛分号分割的statement一般。特别的,initialization 中只能定义同型变量。

这里,再说无穷循环。根据Bruce所说,一般而言Java编译器将while(true) and for(;;) 视为相同,所以如何取舍完全是个人编程口味而已。就我而言,更倾向于使用while(true) 。原因很简单,语义一目了然。

关于break和continue,只是有一点要强调一下:如果使用label,那么break跳出label所描述的循环,而continue则会掉到label处而后重新进入循环。说实话,我真的很不喜欢label。。。

Switch
接着提一下switch,这个控制结构我从前比较少用到,因为觉得只可以对int值做判断不太方便。但是不久前写的一个windows窗口程序及另一个Java Applet程序的事件处理部分,发现switch还是非常便利的。有一点,就是如果某个case后的表达式符合后会一直执行之后的statement直到break语句或者结束。原本觉得这是个麻烦,如《Java in Nutshell》这类书只是提及语言规范却没有足够的说明,而Bruce Eckel则亲切地指出了一个利用此特性的技巧:让多个case堆叠起来,造成“只要吻合其中任何一个条件,便都执行相同的程序代码”的效果。 例如:

ExpandedBlockStart.gifContractedBlock.gif/**//*
InBlock.gif * Created on 2004-11-25
InBlock.gif *
InBlock.gif * TODO To change the template for this generated file go to
InBlock.gif * Window - Preferences - Java - Code Style - Code Templates
ExpandedBlockEnd.gif 
*/

None.gifpackage com.smilereader.test;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**//**
InBlock.gif * @author Sinbadblue Kong
InBlock.gif *
InBlock.gif * TODO To change the template for this generated type comment go to
InBlock.gif * Window - Preferences - Java - Code Style - Code Templates
ExpandedBlockEnd.gif 
*/

ExpandedBlockStart.gifContractedBlock.gif
public class OddAndEven dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static boolean isOdd(int i)dot.gif{
InBlock.gif        
int j = i % 10;
InBlock.gif        boolean result 
= false;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
switch(j)dot.gif{
InBlock.gif        
case 1:
InBlock.gif        
case 3:
InBlock.gif        
case 5:
InBlock.gif        
case 7:
ExpandedSubBlockStart.gifContractedSubBlock.gif        
case 9:dot.gif{
InBlock.gif            result 
= true;
InBlock.gif            System.
out.println("is odd");            
InBlock.gif            
break;            
ExpandedSubBlockEnd.gif        }

InBlock.gif        
default:
InBlock.gif            System.
out.println("is not odd");
ExpandedSubBlockEnd.gif        }

InBlock.gif        
return result;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

恩,虽然还没有熟悉JUnit,但是还是写个简陋的测试吧。恩,有时间一定要仔细看看JUnit的doc。似乎我还下决心看完Tomcat的doc?.....-_-b
ExpandedBlockStart.gifContractedBlock.gif/**//*
InBlock.gif * Created on 2004-11-25
InBlock.gif *
InBlock.gif * TODO To change the template for this generated file go to
InBlock.gif * Window - Preferences - Java - Code Style - Code Templates
ExpandedBlockEnd.gif 
*/
None.gifpackage com.smilereader.test;
None.gif
None.gifimport junit.framework.TestCase;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**//**
InBlock.gif * @author Sinbadblue Kong
InBlock.gif *
InBlock.gif * TODO To change the template for this generated type comment go to
InBlock.gif * Window - Preferences - Java - Code Style - Code Templates
ExpandedBlockEnd.gif 
*/

ExpandedBlockStart.gifContractedBlock.gif
public class OddAndEvenTest extends TestCase dot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void testIsOdd() dot.gif{
InBlock.gif        assertEquals(
true,OddAndEven.isOdd(3));
InBlock.gif        assertEquals(
false,OddAndEven.isOdd(8));
InBlock.gif        assertEquals(
false,OddAndEven.isOdd(0));        
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif


最后捎带一句Math.random( )返回值的范围是: [0,1)

BTW,最后的最后,发现JDK1.4和1.3变化太大了。因为,决定放弃手上的中文版砖头书,以不怕视力下降的魄力开始啃Thinking in Java, 3rd ed. Revision 4.0

转载于:https://www.cnblogs.com/sinbadblue/archive/2004/11/25/68745.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值