java 转整型 哪种方法速度最快?

来源:互联网 发布:腰带软件 过安检 编辑:程序博客网 时间:2024/05/24 03:53
public class NumberFormatTest {public static void main(String[] args) throws Exception {String time = "20";long start = System.currentTimeMillis();for (int i = 0; i < 100000; i++) {//1// return type intNumberFormat.getIntegerInstance().parse(time).intValue();}System.out.println(System.currentTimeMillis() - start);long start2 = System.currentTimeMillis();for (int j = 0; j < 100000; j++) {//2// return type intInteger.parseInt(time);}System.out.println(System.currentTimeMillis() - start2);long start3 = System.currentTimeMillis();for (int k = 0; k < 100000; k++) {//3// return type IntegerInteger.valueOf(time);}System.out.println(System.currentTimeMillis() - start3);}}


运行几次的结果看来,2,3 是比较快的。。我通常用第二种方法。