虽然jdk1.5已经发布很长时间了,由于公司整体产品的jdk并没有进行同步升级,所以最近才开始关注jdk1.5的一些新的特性.
1、泛型(generic)
Collection<String> c = new ArrayList();
c.add(new Date());
//此时编译器便会提出编译错误,此处只能add字符类型.2、增强的For循环
void processAll(Collection c)...{
for(Iterator i=c.iterator(); i.hasNext();)...{
MyClass myObject = (MyClass)i.next();
myObject.process();
}
}
//使用For-Each循环,我们可以把代码改写成:
void processAll(Collection<MyClass> c)...{
for (MyClass myObject :c)
myObject.process();
}
for(Iterator i=c.iterator(); i.hasNext();)...{
MyClass myObject = (MyClass)i.next();
myObject.process();
}
}
//使用For-Each循环,我们可以把代码改写成:
void processAll(Collection<MyClass> c)...{
for (MyClass myObject :c)
myObject.process();
}
3、自动装箱/拆箱(Autoboxing/unboxing)
int a = 3;
Collection c = new ArrayList();
c.add(a);//自动转换成Integer.
Integer b = new Integer(2);
c.add(b +2);
Collection c = new ArrayList();
c.add(a);//自动转换成Integer.
Integer b = new Integer(2);
c.add(b +2);
本文介绍了 JDK 1.5 中引入的重要新特性,包括泛型(generic)、增强的 For 循环以及自动装箱/拆箱(Autoboxing/unboxing)。通过这些特性,开发者可以更高效地编写 Java 代码。
513

被折叠的 条评论
为什么被折叠?



