JDK1.5新特性
"JDK1.5"的一个重要主题就是通过新增一些特性来简化开发,这些特性包括:泛型,for-each 循环, 自动装箱/拆箱,枚举,可变参数, 静态导入,注解, 格式化输出。使用这些特性有助于我们编写更加清晰,精悍,安全的代码。
JDK1.5新特性之泛型(Generic)
泛型只是编译时的概念。供编译器进行语法检查时用的。目的是努力将运行时异常转换为编译时错误,解决模板编程问题。示例如下:
- import java.util.Date;
- /**
- * JDK1.5新特性验证
- * 泛型
- *
- * @author cKF61853
- * @version 1.0, 2012-11-25
- */
- public class Demo
- {
- public static <T> T create(Class<T> cls)
- {
- try
- {
- return cls.newInstance();
- }
- catch (InstantiationException e)
- {
- e.printStackTrace();
- }
- catch (IllegalAccessException e)
- {
- e.printStackTrace();
- }
- return null;
- }
- public static void main(String[] args)
- {
- Date d = Demo.create(Date.class);
- System.out.println(d);
- StringBuffer str = Demo.create(StringBuffer.class);
- System.out.println(str.getClass().getName());
- }
- }
Fri Nov 30 16:33:49 CST 2012
java.lang.StringBuffer
JDK1.5新特性之增强for循环(Enhanced "for" loop)
遍历数组或者集合中的元素,减少编程量。示例如下:
- public static void main(String[] args)
- {
- Collection<String> c = new ArrayList<String>();
- c.add("aa");
- c.add("bb");
- for (String str : c)
- {
- System.out.println(str);
- }
- }
aa
bb
JDK1.5新特性之自动拆箱/装箱(Automatic boxing/unboxing)
- Integer i = new Integer(2);
- //i自动拆箱为int类型
- System.out.println(i == 2);
- //3自动装箱为Integer类型
- System.out.println(i.equals(3));
JDK1.5新特性之可变参数(Variable parameters)
方法的参数是不固定的我们一般会使用重载或者使用数组参数。重载需要些更多写更多的方法,数组需要在使用时先声明。可变参数是一个不错的解决方案。示例如下:
- public static void main(String[] args)
- {
- VariableDemo vd = new VariableDemo();
- vd.speak("悟空", "人和妖精都是妈生的,");
- vd.speak("悟空", "人和妖精都是妈生的,", "妖是妖他妈生的,", "N...", 123456);
- }
- public void speak(String name, Object... arguments)
- {
- System.out.print(name + ": ");
- for (Object object : arguments)
- {
- System.out.print(object);
- }
- System.out.println();
- }
悟空: 人和妖精都是妈生的,
悟空: 不同的人是人他妈生的,妖是妖他妈生的,N ...123456
JDK1.5新特性之注解(Annotation)
jdk1.5以后,系统提供单个 annotation
@Override 用来说明一个方法是覆写方法,并检查其是否符合覆写规则。
@Deprecated 用来说明某个类或者方法已经不建议使用,提供警告信息。
@SuppressWarnings(value) 用来压制某个警告信息。
JDK1.5新特性之枚举(Enumerate)
当每一类型可取值范围是有限的时候,可以使用枚举,例如每个学生登记只能用ABCD表示,如果直接用E的话,那么编译不会出错,但是却不符合输入要求,而使用枚举增加程序的易读性和健壮性。
JDK1.5新特性之格式化输出(Formatted print)
C语言中printf()风格的格式化输出。示例如下:
- public static void main(String[] args)
- {
- int x = 5;
- double y = 5.332542;
- //The old way
- System.out.println("Row 1: ["+x+" "+y+"]");
- //The new way
- System.out.format("Row 2: [%d %f]\n", x,y);
- //or
- System.out.printf("Row 3: [%d %f]\n", x, y);
- }
JDK1.5新特性之静态导入(Static import)
静态导入允许您在调用其它类中定义的静态成员时,可以忽略类名。实例代码如下:
- //静态成员定义
- package cn.j5newfeatures.staticimport;
- /**
- * JDK1.5新特性验证
- * 静态导入
- *
- * @author cKF61853
- * @version 1.0, 2012-2-9
- */
- public class StaticImportInstance
- {
- public static final int A = 10;
- public static final double B = 20.00;
- public static final double C = 30.00;
- public static double add (double one, double two)
- {
- return one + two;
- }
- public static double adder (double one, double two, int three)
- {
- return one + two + three;
- }
- public static void print(String str)
- {
- System.out.println("OMG:" + str);
- }
- }
- //静态成员调用
- package cn.j5newfeatures.staticimport;
- //此处部分是关键
- import static cn.j5newfeatures.staticimport.StaticImportInstance.A;
- import static cn.j5newfeatures.staticimport.StaticImportInstance.B;
- import static cn.j5newfeatures.staticimport.StaticImportInstance.C;
- import static cn.j5newfeatures.staticimport.StaticImportInstance.add;
- import static cn.j5newfeatures.staticimport.StaticImportInstance.adder;
- import static cn.j5newfeatures.staticimport.StaticImportInstance.print;
- /**
- * JDK1.5新特性验证
- * 静态导入测试
- *
- * @author cKF61853
- */
- public class TestStaticImport
- {
- public static void main(String[] args)
- {
- print("A的值是" + A);
- print("B的值是" + B);
- print("C的值是" + C);
- System.out.println(add(A, B));
- System.out.println(adder(B, C, A));
- print("静态导入测试...");
- }
- }
OMG:C的值是30.0
30.0
60.0
OMG:静态导入测试...