在for循环进行类型转换

 最近在开发中想到一个问题,在for循环中String进行转换int类型,或者在循环外边有什么区别,在循环次数不是很多的情况下,效率上是没有影响的,下面是本人做了一个小例子,通过例子说明,不要在循环中进行类型转换,编程从细节开始。

public static void main(String[] args) {
		String str = "1";
		System.out.println("start"+new Date());
		for (int i = 0; i < 1000000000; i++) {
			int stt = Integer.valueOf(str);
		}
		System.out.println("end"+new Date());
}

 测试结果

startThu Jan 24 10:13:20 CST 2019
endThu Jan 24 10:13:24 CST 2019
public static void main(String[] args) {
        String str = "1";
        int stt = Integer.valueOf(str);
        System.out.println("start"+new Date());
        for (int i = 0; i < 1000000000; i++) {
            int strs = stt;
        }
        System.out.println("end"+new Date());
}

测试结果

startThu Jan 24 10:18:15 CST 2019
endThu Jan 24 10:18:15 CST 2019

猜你喜欢

转载自blog.csdn.net/qq_31984879/article/details/86621365