java 陷阱,java 中的陷阱。
看了一段北風(fēng)網(wǎng)的視頻,總結(jié)幾個(gè)經(jīng)典的java陷阱給大家。
答案在博客中:http://blog.csdn.net/ol_beta/archive/2010/05/17/5598867.aspx
歡迎大家討論!
1、找奇數(shù):
Java code
public static boolean isOdd(int i){
return i % 2 == 0;
}
上面的方法真的能找到所有的奇數(shù)么?
2、浮點(diǎn)數(shù)想減
Java code
System.out.println(2.0-1.9);
System.out.println(2.0-1.9);
上面會(huì)打印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');
上面兩個(gè)語句輸出結(jié)果相同么?
5、無限循環(huán)
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);
}
上面程序運(yùn)行的結(jié)果是什么?
6、計(jì)數(shù)器問題
Java code
int minutes = 0;
for (int ms = 0; ms < 60*60*1000; ms++)
if (ms % 60*1000 == 0)
minutes++;
System.out.println(minutes);
結(jié)果跟你想的一樣么?
7、到底返回什么?
Java code
public static boolean decision() {
try {
return true;
} finally {
return false;
}
}
true?false?
8、錯(cuò)誤里聚集遍歷
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)
總結(jié)
以上是生活随笔為你收集整理的java 陷阱,java 中的陷阱。的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android计算器弹窗,android
- 下一篇: 爬虫找工作要掌握什么_找爬虫工作必须掌握