java puzzlers---java 陷阱题

从BBS上看的一个帖子,摘录几条下来:

看了一段北风网的视频,总结几个经典的java陷阱给大家。

建议先思考一下结果,然后运行代码试验。也许你会恍然大悟。

1、找奇数:


public static boolean isOdd(int i){   
         return i % 2 == 1;   
    } 
public static boolean isOdd(int i){
   return i % 2 == 1;
 }

上面的方法真的能找到所有的奇数么?

A:没有考虑到负数问题,如果传参是负数,那么永远不能得到结果!应该是:return i % 2 != 0;

2、浮点数想减

System.out.println(2.0-1.9); 
System.out.println(2.0-1.9);

上面会打印0.1么?

A:不会,自己试验就知道结果了。正确做法:用decimal。

3、交换


int x = 2010;   
int y = 2012;   
x^=y^=x^=y;  
System.out.println("x= " + x + "; y= " + y); 
int x = 2010;
int y = 2012;
x^=y^=x^=y;
System.out.println("x= " + x + "; y= " + y);

x、y的值呼唤了么?

A:没有,java运算顺序是从左到右的,应该这么写:y=(x^= (y^= x))^ y;

4、字符和字符串

+ expand source
System.out.println("H" + "a");  
System.out.println('H' + 'a'); 
System.out.println("H" + "a");
System.out.println('H' + 'a');

上面两个语句输出结果相同么?

A:不想同,字符会被转换成在数字。所以第一句输出:Ha,第二句输出两个字符的assii码相加的数字。

5、无限循环


public static final int END = Integer.MAX_VALUE;   
public static final int START = END - 100;   
public static void main(String[] args) {  
    int count = 0;   
    for (int i = START; i <= END; i++)   
        count++;   
    System.out.println(count);   
    } 
public static final int END = Integer.MAX_VALUE;
public static final int START = END - 100;
public static void main(String[] args) {
 int count = 0;
 for (int i = START; i <= END; i++)
  count++;
 System.out.println(count);
 }

上面程序运行的结果是什么?

A:无限循环。将i<=END改成i<END?为什么呢?你知道的,呵呵!

6、计数器问题


int minutes = 0;   
for (int ms = 0; ms < 60*60*1000; ms++)   
    if (ms % 60*1000 == 0)   
        minutes++;   
System.out.println(minutes); 
int minutes = 0;
for (int ms = 0; ms < 60*60*1000; ms++)
 if (ms % 60*1000 == 0)
     minutes++;
System.out.println(minutes);

结果跟你想的一样么?

A:呵呵,括号问题,不多说!

7、到底返回什么?


public static boolean decision() {   
     try {   
        return true;   
    } finally {   
      return false;   
    }   
}  
public static boolean decision() {
  try {
     return true;
 } finally {
   return false;
 }

true?false?

A:一般情况下,不管怎么说try/catch代码块中,finally总是最后被执行的 。

8、错误里聚集遍历


public static void main(String[] args) {  
        Vector v = new Vector();  
        v.add("one");  
        v.add("two");  
        v.add("three");   
        v.add("four");  
        Enumeration enume = v.elements();  
        while (enume.hasMoreElements()){  
            String s = (String) enume.nextElement();  
            if (s.equals("two"))  
                v.remove("two");  
            else{  
                System.out.println(s);  
            }  
        }  
        System.out.println("What's really there...");  
        enume = v.elements();  
        while (enume.hasMoreElements()){  
            String s = (String) enume.nextElement();  
            System.out.println(s);              
        }  
    } 
public static void main(String[] args) {
        Vector v = new Vector();
        v.add("one");
        v.add("two");
        v.add("three");
        v.add("four");
        Enumeration enume = v.elements();
        while (enume.hasMoreElements()){
            String s = (String) enume.nextElement();
            if (s.equals("two"))
                v.remove("two");
            else{
                System.out.println(s);
            }
        }
        System.out.println("What's really there...");
        enume = v.elements();
        while (enume.hasMoreElements()){
            String s = (String) enume.nextElement();
            System.out.println(s);           
        }
 }

运行代码看看结果跟你想的一样么?

A:一般不建议在遍历聚集的时候对聚集进行操作。为什么结果是这样呢?看JDK源码能得到答案。Enumeration没有实现Fail Fast操作,如果换成ArrayList,上面的代码可能会出错。《java与模式》迭代子(iterator)介绍了。

 

 

有兴趣的,可以看看 <java puzzlers>这本书,上面的应该都出去这书,很经典的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值