易截截图软件、单文件、免安装、纯绿色、仅160KB

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中集合容器类List和Set的用法

List的用法
List包括List接口以及List接口的所有实现类。因为List接口实现了Collection接口,所以List接口拥有Collection接口提供的所有常用方法,又因为List是列表类型,所以List接口还提供了一些适合于自身的常用方法,如表1所示。
表1  List接口定义的常用方法及功能
从表1可以看出,List接口提供的适合于自身的 ......

JNA实现Java调用Fortran

在成功实现Java调用C++之后,接下来想到能否通过JNA实现Java调用Fortran,今天试验了一下,还是比较容易的。
网上有一个Java调用F95的例子,但是我考虑不仅要实现F95的调用,还要实现F77的调用,所以费了一些周折。
问题的关键在于F77为过程名自动添加了一个尾部的下划线,所以sub1这个过程,到Java一端,就变成了sub1_, ......

java中 string类型转换成UTF 8

1、测试方法如下:
          public static String toUtf8(String str) {
               return new String(str.getBytes("UTF-8"),"UTF-8"):
       &nb ......

Heritrix网络爬虫对BDB嵌入式数据库的应用(JAVA)

Heritrix网络爬虫对BDB嵌入式数据库的应用(JAVA)
一、Heritrix中和BDB相关的类
org.archive.bdb.BdbModule 用来映射一个共享的BDB数据库环境(JE)的通用模型。
org.archive.bdb.BdbModule.BdbConfig 数据库配置对象。必需的,因为com.sleepycat.je.DatabaseConfig这个对象是不会被序列化的。此外它还可以防止无效设置 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号