package com.mysoft.test.type.boxing;
/**
* @Title: Autoboxing.java
* @Package com.mysoft.test.type.boxing
* @Description: TODO()
* @author wwl
* @date 2016年6月29日 下午5:06:26
* @version V1.0
*/
public class Autoboxing {
public static void main(String[] args) {
autoboxing();
unboxing();
}
/**
* 自动装箱
*/
public static void autoboxing() {
long t = System.currentTimeMillis();
Long sum = 0L;
for (long i = 0; i < Integer.MAX_VALUE; i++) {
//sum += new Long(i);//等同
sum += i;
}
System.out.println("total:" + sum);
System.out.println("processing time: " + (System.currentTimeMillis() - t) + " ms");
}
/**
*不是用自动装箱
*/
public static void unboxing() {
long t = System.currentTimeMillis();
long sum = 0L;
for (long i = 0; i < Integer.MAX_VALUE; i++) {
sum += i;
}
System.out.println("total:" + sum);
System.out.println("processing time: " + (System.currentTimeMillis() - t) + " ms");
}
//jdk1.6
// total:2305843005992468481
// processing time: 67017 ms
// total:2305843005992468481
// processing time: 14518 ms
// total:2305843005992468481
// processing time: 68589 ms
// total:2305843005992468481
// processing time: 15100 ms
//jkd1.7
// total:2305843005992468481
// processing time: 31885 ms
// total:2305843005992468481
// processing time: 3827 ms
// total:2305843005992468481
// processing time: 30350 ms
// total:2305843005992468481
// processing time: 3675 ms
}
使用装箱严重影响性能,idk8暂未测试