现在的位置: 首页 > 综合 > 正文

JDK1.5之 新特性

2018年05月19日 ⁄ 综合 ⁄ 共 4516字 ⁄ 字号 评论关闭

JDK1.5新特性

"JDK1.5"的一个重要主题就是通过新增一些特性来简化开发,这些特性包括:泛型,for-each 循环, 自动装箱/拆箱,枚举,可变参数静态导入,注解, 格式化输出。使用这些特性有助于我们编写更加清晰,精悍,安全的代码。

JDK1.5新特性之泛型(Generic)

泛型只是编译时的概念。供编译器进行语法检查时用的。目的是努力将运行时异常转换为编译时错误,解决模板编程问题。示例如下:

  1. import java.util.Date;  
  2.   
  3. /** 
  4.  * JDK1.5新特性验证 
  5.  * 泛型 
  6.  *  
  7.  * @author  cKF61853 
  8.  * @version  1.0, 2012-11-25 
  9.  */  
  10. public class Demo  
  11. {  
  12.     public static <T> T create(Class<T> cls)  
  13.     {  
  14.         try  
  15.         {  
  16.             return cls.newInstance();  
  17.         }  
  18.         catch (InstantiationException e)  
  19.         {  
  20.             e.printStackTrace();  
  21.         }  
  22.         catch (IllegalAccessException e)  
  23.         {  
  24.             e.printStackTrace();  
  25.         }  
  26.         return null;  
  27.     }  
  28.       
  29.     public static void main(String[] args)  
  30.     {  
  31.         Date d = Demo.create(Date.class);  
  32.         System.out.println(d);  
  33.           
  34.         StringBuffer str = Demo.create(StringBuffer.class);  
  35.         System.out.println(str.getClass().getName());  
  36.     }  
  37.       
  38. }  

//控制台输出
Fri Nov 30 16:33:49 CST 2012
java.lang.StringBuffer

JDK1.5新特性之增强for循环(Enhanced "for" loop)

遍历数组或者集合中的元素,减少编程量。示例如下:

  1. public static void main(String[] args)  
  2. {  
  3.         Collection<String> c = new ArrayList<String>();  
  4.         c.add("aa");  
  5.         c.add("bb");  
  6.         for (String str : c)  
  7.         {  
  8.               System.out.println(str);  
  9.         }  
  10. }  

//控制台输出
aa
bb

JDK1.5新特性之自动拆箱/装箱(Automatic boxing/unboxing)

  1. Integer i = new Integer(2);  
  2.   
  3. //i自动拆箱为int类型  
  4. System.out.println(i == 2);  
  5.   
  6. //3自动装箱为Integer类型  
  7. System.out.println(i.equals(3));  

JDK1.5新特性之可变参数(Variable parameters)

方法的参数是不固定的我们一般会使用重载或者使用数组参数。重载需要些更多写更多的方法,数组需要在使用时先声明。可变参数是一个不错的解决方案。示例如下:

  1. public static void main(String[] args)  
  2. {  
  3.         VariableDemo vd = new VariableDemo();  
  4.         vd.speak("悟空""人和妖精都是妈生的,");  
  5.     vd.speak("悟空""人和妖精都是妈生的,""妖是妖他妈生的,""N..."123456);  
  6. }  
  7.       
  8. public void speak(String name, Object... arguments)  
  9. {  
  10.         System.out.print(name + ": ");  
  11.         for (Object object : arguments)  
  12.         {  
  13.               System.out.print(object);  
  14.         }  
  15.         System.out.println();  
  16. }  

//控制台输出
悟空: 人和妖精都是妈生的,
悟空: 不同的人是人他妈生的,妖是妖他妈生的,N ...123456

JDK1.5新特性之注解(Annotation)

jdk1.5以后,系统提供单个 annotation
@Override 用来说明一个方法是覆写方法,并检查其是否符合覆写规则。
@Deprecated 用来说明某个类或者方法已经不建议使用,提供警告信息。
@SuppressWarnings(value)  用来压制某个警告信息。

JDK1.5新特性之枚举(Enumerate)

当每一类型可取值范围是有限的时候,可以使用枚举,例如每个学生登记只能用ABCD表示,如果直接用E的话,那么编译不会出错,但是却不符合输入要求,而使用枚举增加程序的易读性和健壮性。

JDK1.5新特性之格式化输出(Formatted print)

C语言中printf()风格的格式化输出。示例如下:

  1. public static void main(String[] args)  
  2. {  
  3.         int x = 5;     
  4.         double y = 5.332542;     
  5.         //The old way     
  6.         System.out.println("Row 1: ["+x+" "+y+"]");     
  7.         //The new way     
  8.         System.out.format("Row 2: [%d %f]\n", x,y);     
  9.         //or     
  10.         System.out.printf("Row 3: [%d %f]\n", x, y);     
  11. }  

JDK1.5新特性之静态导入(Static import)
静态导入允许您在调用其它类中定义的静态成员时,可以忽略类名。实例代码如下:

  1. //静态成员定义  
  2. package cn.j5newfeatures.staticimport;  
  3.   
  4. /** 
  5.  * JDK1.5新特性验证 
  6.  * 静态导入 
  7.  *  
  8.  * @author  cKF61853 
  9.  * @version  1.0, 2012-2-9 
  10.  */  
  11. public class StaticImportInstance  
  12. {  
  13.     public static final int A = 10;  
  14.     public static final double B = 20.00;  
  15.     public static final double C = 30.00;  
  16.       
  17.     public static double add (double one, double two)  
  18.     {  
  19.         return one + two;  
  20.     }  
  21.       
  22.     public static double adder (double one, double two, int three)  
  23.     {  
  24.         return one + two + three;  
  25.     }  
  26.       
  27.     public static void print(String str)  
  28.     {  
  29.         System.out.println("OMG:" + str);  
  30.     }  
  31. }  
  32.   
  33. //静态成员调用  
  34. package cn.j5newfeatures.staticimport;  
  35.   
  36. //此处部分是关键  
  37. import static cn.j5newfeatures.staticimport.StaticImportInstance.A;  
  38. import static cn.j5newfeatures.staticimport.StaticImportInstance.B;  
  39. import static cn.j5newfeatures.staticimport.StaticImportInstance.C;  
  40. import static cn.j5newfeatures.staticimport.StaticImportInstance.add;  
  41. import static cn.j5newfeatures.staticimport.StaticImportInstance.adder;  
  42. import static cn.j5newfeatures.staticimport.StaticImportInstance.print;  
  43.   
  44. /** 
  45.  * JDK1.5新特性验证 
  46.  * 静态导入测试 
  47.  *  
  48.  * @author  cKF61853 
  49.  */  
  50. public class TestStaticImport  
  51. {  
  52.     public static void main(String[] args)  
  53.     {  
  54.         print("A的值是" + A);  
  55.         print("B的值是" + B);  
  56.         print("C的值是" + C);  
  57.           
  58.         System.out.println(add(A, B));  
  59.         System.out.println(adder(B, C, A));  
  60.           
  61.         print("静态导入测试...");  
  62.     }  
  63.       
  64. }  

//控制台输出
OMG:C的值是30.0
30.0
60.0
OMG:静态导入测试...

抱歉!评论已关闭.