看了一段北风网的视频,总结几个经典的java陷阱给大家。
答案在我的博客中:http://blog.csdn.net/ol_beta/archive/2010/05/17/5598867.aspx
欢迎大家讨论!1、找奇数:
public static boolean isOdd(int i){
return i % 2 == 0;
}
上面的方法真的能找到所有的奇数么?2、浮点数想减
System.out.println(2.0-1.9);
System.out.println(2.0-1.9);
上面会打印0.1么?3、交换int x = 2010;
int y = 2012;
x^=y^=x^=y;
System.out.println("x= " + x + "; y= " + y);x、y的值呼唤了么?4、字符和字符串
System.out.println("H" + "a");
System.out.println('H' + 'a'); 上面两个语句输出结果相同么?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);
}
上面程序运行的结果是什么?6、计数器问题int minutes = 0;
for (int ms = 0; ms < 60*60*1000; ms++)
if (ms % 60*1000 == 0)
minutes++;
System.out.println(minutes);
结果跟你想的一样么?7、到底返回什么?public static boolean decision() {
try {
return true;
} finally {
return false;
}
}
true?false?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);
}
}
运行代码看看结果跟你想的一样么?
答案在我的博客中:http://blog.csdn.net/ol_beta/archive/2010/05/17/5598867.aspx
欢迎大家讨论!1、找奇数:
public static boolean isOdd(int i){
return i % 2 == 0;
}
上面的方法真的能找到所有的奇数么?2、浮点数想减
System.out.println(2.0-1.9);
System.out.println(2.0-1.9);
上面会打印0.1么?3、交换int x = 2010;
int y = 2012;
x^=y^=x^=y;
System.out.println("x= " + x + "; y= " + y);x、y的值呼唤了么?4、字符和字符串
System.out.println("H" + "a");
System.out.println('H' + 'a'); 上面两个语句输出结果相同么?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);
}
上面程序运行的结果是什么?6、计数器问题int minutes = 0;
for (int ms = 0; ms < 60*60*1000; ms++)
if (ms % 60*1000 == 0)
minutes++;
System.out.println(minutes);
结果跟你想的一样么?7、到底返回什么?public static boolean decision() {
try {
return true;
} finally {
return false;
}
}
true?false?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);
}
}
运行代码看看结果跟你想的一样么?
第一个写错了 - -!
惊人!
4:单引号是CHAR吧
5:I会溢出变为-2147483648,所以无限循环
@ 史建敏 Test
*/
public static boolean isOdd(int i) {
return (i & 0x1) != 0;
}
2. 这个double类型的问题, 输出应该是很多位的小数, 很多书上都提到过.
3. 这个不懂, 等下研究.
4. 自动装箱的处理吧, 这个字符串和字符的处理是不一样的, 就像打印1+2一样.
5. 数值到了最大后加一自动成为最小了, 顶多也就等于最大, 所以永远也退不出循环.
6. 应该是60000. 只要是60的倍数就加一次.
7. false, finally 里面的语句, 一定会执行.
8. 这个也不懂, 等下研究.
<instreet id="instreet" name="instreet" value="4ZILXB6w" />
2.不能,可以把它转化成更小的单位
3.x= 0; y= 2010
4.Ha 169
5.木有结果
6.60000
7.false