JDK 1.5 新特性——自动拆箱装箱

  

  1.5版本支持了自动包装和解包操作,对于bool/Boolean,byte/Byte,double/Double,short/Short,int/Integer,

long/Long,float/Float的相应包装/解包操作都进行了支持,从而使代码变得简单。

  这里需要注意的是,基本数据类型和类类型是不一样的!

 

1.5版本以前的版本写法是:
1 Vector v=new Vector();
2 v.add(new Integer(1)); //手动装箱
3 int a= Integer.valueOf((Integer)(v.get(0))); //手动拆箱
而在1.5版本中可以写为:
Vector<Integer> v=new Vector<Integer>(1);//这里同时用到了泛型
v.add(1); //自动装箱
int a=v.get(0); //自动拆箱
posted @ 2012-07-25 00:04  yokoboy  阅读(279)  评论(0编辑  收藏  举报
yokoboy