【郭林专刊】java中的陷阱

 

总结几个经典的java陷阱给大家。

答案隐藏了,Ctrl+A显示。建议先思考一下结果,然后运行代码试验。也许你会恍然大悟。

1、找奇数:

Java代码 复制代码  收藏代码
  1. public static boolean isOdd(int i){    
  2.          return i % 2 == 1;    
  3.     }  
public static boolean isOdd(int i){ 
		 return i % 2 == 1; 
	}
 

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

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

2、浮点数想减

Java代码 复制代码  收藏代码
  1. System.out.println(2.0-1.9);  
System.out.println(2.0-1.9);
 

 

上面会打印0.1么?

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

3、交换

Java代码 复制代码  收藏代码
  1. int x = 2010;    
  2. int y = 2012;    
  3. x^=y^=x^=y;   
  4. 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、字符和字符串

Java代码 复制代码  收藏代码
  1. System.out.println("H" + "a");   
  2. System.out.println('H' + 'a');  
System.out.println("H" + "a");
System.out.println('H' + 'a');
  

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

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

5、无限循环

Java代码 复制代码  收藏代码
  1. public static final int END = Integer.MAX_VALUE;    
  2. public static final int START = END - 100;    
  3. public static void main(String[] args) {   
  4.     int count = 0;    
  5.     for (int i = START; i <= END; i++)    
  6.         count++;    
  7.     System.out.println(count);    
  8.     }  
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、计数器问题

Java代码 复制代码  收藏代码
  1. int minutes = 0;    
  2. for (int ms = 0; ms < 60*60*1000; ms++)    
  3.     if (ms % 60*1000 == 0)    
  4.         minutes++;    
  5. 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、到底返回什么?

Java代码 复制代码  收藏代码
  1. public static boolean decision() {    
  2.      try {    
  3.         return true;    
  4.     } finally {    
  5.       return false;    
  6.     }    
  7. }   
public static boolean decision() { 
	 try { 
	    return true; 
	} finally { 
	  return false; 
	} 
} 
 

true?false?

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

8、错误里聚集遍历

Java代码 复制代码  收藏代码
  1. public static void main(String[] args) {   
  2.         Vector v = new Vector();   
  3.         v.add("one");   
  4.         v.add("two");   
  5.         v.add("three");   
  6.         v.add("four");   
  7.         Enumeration enume = v.elements();   
  8.         while (enume.hasMoreElements()) {   
  9.             String s = (String) enume.nextElement();   
  10.             if (s.equals("two"))   
  11.                 v.remove("two");   
  12.             else {   
  13.                 System.out.println(s);   
  14.             }   
  15.         }   
  16.         System.out.println("What's really there...");   
  17.         enume = v.elements();   
  18.         while (enume.hasMoreElements()) {   
  19.             String s = (String) enume.nextElement();   
  20.             System.out.println(s);   
  21.         }   
  22.     }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值