Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2338449
  • 博文数量: 2110
  • 博客积分: 18861
  • 博客等级: 上将
  • 技术积分: 24420
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-05 18:23
文章分类

全部博文(2110)

文章存档

2011年(139)

2010年(1971)

我的朋友

分类: Java

2010-09-30 10:51:08

 /**

  *

  * The Advantages and Traps of Autoboxing

  *

  * 优点:

  * 1. 代码更简洁

  * 2. 自动使用最优的转换代码(比如使用Integer.valueOf(int),而不是使用new Integer(int))

  *

  * 缺点:

  * 1. 如果不注意,可能会导致错误

  * 2. 如果不注意,可能会导致效率低下

  * 3. 有时需要强制类型转换。

  *

  * 参考:

  *

  */

  public class M {

  public static void main(String[] args) {

  // 导致错误

  Long longValue1 = 99L;

  System.out.println(longValue1.equals(99L)); // true

  System.out.println(longValue1.equals(99)); // false

  System.out.println(longValue1 == 99L); // true

  System.out.println(longValue1 == 99); // true

  long t1;

  long t2;

  // 导致效率低下

  // 实验组 1

  t1 = System.currentTimeMillis();

  Long counter1 = 0L; // Long counter = 0; 会报错!!!

  for (long i = 0; i < 100000000; i++) {

  counter1++; // 等价于 Long.valueOf(counter1.longValue() + 1)

  }

  t2 = System.currentTimeMillis();

  System.out.println("time1 = " + (t2 - t1)); // 1625

  // 实验组 2

  t1 = System.currentTimeMillis();

  long counter2 = 0;

  for (long i = 0; i < 100000000; i++) {

  counter2++;

  }

  t2 = System.currentTimeMillis();

  System.out.println("time2 = " + (t2 - t1)); // 297

  // Integer : 1 + 2 = 3

  // 需强制类型转换,否则调用printSum(long, long)

  // 强制转换为 int也会调用printSum(long, long)

  printSum((Integer) 1, (Integer) 2);

  // Long1 : 1 + 2 = 3

  printSum(1L, 2L);

  // 需强制类型转换

  // Long2 : 1 + 2 = 3

  printSum((Long) 1L, (Long) 2L);

  // Float : 1.0 + 2.0 = 3.0

  printSum(1F, 2F);

  // Double : 1.0 + 2.0 = 3.0

  printSum(1D, 2D);

  }

  public static void printSum(long a, long b) {

  System.out.println("Long1 : " + a + " + " + b + " = " + (a + b)); // true

  }

  public static void printSum(Long a, Long b) {

  System.out.println("Long2 : " + a + " + " + b + " = " + (a + b)); // true

  }

  public static void printSum(Integer a, Integer b) {

  System.out.println("Integer : " + a + " + " + b + " = " + (a + b)); // true

  }

  public static void printSum(Float a, Float b) {

  System.out.println("Float : " + a + " + " + b + " = " + (a + b)); // true

  }

  public static void printSum(Double a, Double b) {

  System.out.println("Double : " + a + " + " + b + " = " + (a + b)); // true

  }

  }

阅读(197) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~