java 陷阱,java 中的陷阱。
看了一段北風網的視頻,總結幾個經典的java陷阱給大家。
答案在博客中:http://blog.csdn.net/ol_beta/archive/2010/05/17/5598867.aspx
歡迎大家討論!
1、找奇數:
Java code
public static boolean isOdd(int i){
return i % 2 == 0;
}
上面的方法真的能找到所有的奇數么?
2、浮點數想減
Java code
System.out.println(2.0-1.9);
System.out.println(2.0-1.9);
上面會打印0.1么?
3、交換
Java code
int x = 2010;
int y = 2012;
x^=y^=x^=y;
System.out.println("x= " + x + "; y= " + y);
x、y的值呼喚了么?
4、字符和字符串
Java code
System.out.println("H" + "a");
System.out.println('H' + 'a');
上面兩個語句輸出結果相同么?
5、無限循環
Java code
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);
}
上面程序運行的結果是什么?
6、計數器問題
Java code
int minutes = 0;
for (int ms = 0; ms < 60*60*1000; ms++)
if (ms % 60*1000 == 0)
minutes++;
System.out.println(minutes);
結果跟你想的一樣么?
7、到底返回什么?
Java code
public static boolean decision() {
try {
return true;
} finally {
return false;
}
}
true?false?
8、錯誤里聚集遍歷
Java code
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)
總結
以上是生活随笔為你收集整理的java 陷阱,java 中的陷阱。的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android计算器弹窗,android
- 下一篇: linux firefox 脚本,lin