jdk1.5新特性总结

来源:互联网 发布:mac输入法快捷键设置 编辑:程序博客网 时间:2024/05/23 00:07

1、泛型(Generic)

可以在编译的时候检测出类型错误,编译后和没有使用泛型的效果是相同的,但是使用泛型可以让你在编译时就发现错误,例如:

例1-1代码 复制代码
  1. import java.util.ArrayList;   
  2. import java.util.Collection;   
  3. import java.util.Iterator;   
  4.   
  5. public class GenericTest {   
  6.     public static void main(String[] args) {   
  7.         Collection c = new ArrayList();   
  8.         c.add(new Integer(1));   
  9.         c.add("123");   
  10.         for(Iterator i=c.iterator();i.hasNext();){   
  11.             String s = (String) i.next();   
  12.             System.out.println(s);   
  13.         }   
  14.     }   
  15. }   
  16.   
  17. 运行结果:   
  18. Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String   
  19.     at GenericTest.main(GenericTest.java:12)  
例1-1代码 复制代码
  1. import java.util.ArrayList;   
  2. import java.util.Collection;   
  3. import java.util.Iterator;   
  4.   
  5. public class GenericTest {   
  6.     public static void main(String[] args) {   
  7.         Collection c = new ArrayList();   
  8.         c.add(new Integer(1));   
  9.         c.add("123");   
  10.         for(Iterator i=c.iterator();i.hasNext();){   
  11.             String s = (String) i.next();   
  12.             System.out.println(s);   
  13.         }   
  14.     }   
  15. }   
  16.   
  17. 运行结果:   
  18. Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String   
  19.     at GenericTest.main(GenericTest.java:12)  

 Collection应该只存放String对象,但是我们“不小心”添加了一个Integer类型的对象,编译正常进行,程序在运行时才发现错误。

下面是使用了泛型后的程序

例1-2代码 复制代码
  1. import java.util.ArrayList;   
  2. import java.util.Collection;   
  3. import java.util.Iterator;   
  4.   
  5. public class GenericTest {   
  6.     public static void main(String[] args) {   
  7.         Collection<String> c = new ArrayList<String>();   
  8.         c.add(new Integer(1));   
  9.         c.add("123");   
  10.         for(Iterator<String> i=c.iterator();i.hasNext();){   
  11.             String s = i.next();   
  12.             System.out.println(s);   
  13.         }   
  14.     }   
  15. }   
  16.   
  17. 运行结果   
  18. D:/test>javac GenericTest.java   
  19. GenericTest.java:8: 无法将 java.util.Collection<java.lang.String> 中的 add(java.lang.String) 应用于 (java.lang.Integer)   
  20.                 c.add(new Integer(1));   
  21.                  ^   
  22. 1 错误   
  23.   
  24. D:/test>  
例1-2代码 复制代码
  1. import java.util.ArrayList;   
  2. import java.util.Collection;   
  3. import java.util.Iterator;   
  4.   
  5. public class GenericTest {   
  6.     public static void main(String[] args) {   
  7.         Collection<String> c = new ArrayList<String>();   
  8.         c.add(new Integer(1));   
  9.         c.add("123");   
  10.         for(Iterator<String> i=c.iterator();i.hasNext();){   
  11.             String s = i.next();   
  12.             System.out.println(s);   
  13.         }   
  14.     }   
  15. }   
  16.   
  17. 运行结果   
  18. D:/test>javac GenericTest.java   
  19. GenericTest.java:8: 无法将 java.util.Collection<java.lang.String> 中的 add(java.lang.String) 应用于 (java.lang.Integer)   
  20.                 c.add(new Integer(1));   
  21.                  ^   
  22. 1 错误   
  23.   
  24. D:/test>  

 

使用了泛型之后在编译时就发现了错误,可以增强程序的健壮性,而其i.next();也不用使用强制类型转换了。

 

2、增强for循环(For-Each)

For-Each的内部是用Iterator实现的,但是使用起来更简单,例如使用For-Each实现1-2如下

例2-1 代码 复制代码
  1. import java.util.ArrayList;   
  2. import java.util.Collection;   
  3.   
  4. public class GenericTest {   
  5.     public static void main(String[] args) {   
  6.         Collection<String> c = new ArrayList<String>();   
  7.         c.add("aa");   
  8.         c.add("bb");   
  9.         for(String s:c){   
  10.             System.out.println(s);   
  11.         }   
  12.     }   
  13. }   
  14. 运行结果:   
  15. aa   
  16. bb  
例2-1 代码 复制代码
  1. import java.util.ArrayList;   
  2. import java.util.Collection;   
  3.   
  4. public class GenericTest {   
  5.     public static void main(String[] args) {   
  6.         Collection<String> c = new ArrayList<String>();   
  7.         c.add("aa");   
  8.         c.add("bb");   
  9.         for(String s:c){   
  10.             System.out.println(s);   
  11.         }   
  12.     }   
  13. }   
  14. 运行结果:   
  15. aa   
  16. bb  

 

比Integer方便多了吧?可以使程序员更加注重逻辑,而不是代码本身。

 

3、自动装箱拆箱(Autoboxing/unboxing)

 

例3-1代码 复制代码
  1. Integer i = new Integer(2);   
  2. //i自动拆箱为int类型   
  3. System.out.println(i==2);   
  4. //3自动装箱为Integer类型   
  5. System.out.println(i.equals(3));  
例3-1代码 复制代码
  1. Integer i = new Integer(2);   
  2. //i自动拆箱为int类型   
  3. System.out.println(i==2);   
  4. //3自动装箱为Integer类型   
  5. System.out.println(i.equals(3));  

 

4、静态导入(static import)

 

例4-1代码 复制代码
  1. //静态导入Math的random方法   
  2. import static java.lang.Math.random;   
  3.   
  4. public class StaticImportTest {   
  5.     public static void main(String[] args){   
  6.         //类中生成随机数数可以直接使用静态引入的random方法了,而不用Math.random()这样调用了   
  7.         System.out.println(random());   
  8.     }   
  9. }  
例4-1代码 复制代码
  1. //静态导入Math的random方法   
  2. import static java.lang.Math.random;   
  3.   
  4. public class StaticImportTest {   
  5.     public static void main(String[] args){   
  6.         //类中生成随机数数可以直接使用静态引入的random方法了,而不用Math.random()这样调用了   
  7.         System.out.println(random());   
  8.     }   
  9. }  

 

5、格式化打印(formatted print)

C语言中printf()风格的格式化输出。

这里只举一个thinking in java的一个例子:

例5-1代码 复制代码
  1. public class SimpleFormat {   
  2.     public static void main(String[] args) {   
  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 1: [%d %f]/n", x,y);   
  9.         //or   
  10.         System.out.printf("Row 1: [%d %f]/n", x, y);   
  11.     }   
  12. }   
  13.   
  14. 运行结果:   
  15. Row 1: [5 5.332542]   
  16. Row 1: [5 5.332542]   
  17. Row 1: [5 5.332542]  
例5-1代码 复制代码
  1. public class SimpleFormat {   
  2.     public static void main(String[] args) {   
  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 1: [%d %f]/n", x,y);   
  9.         //or   
  10.         System.out.printf("Row 1: [%d %f]/n", x, y);   
  11.     }   
  12. }   
  13.   
  14. 运行结果:   
  15. Row 1: [5 5.332542]   
  16. Row 1: [5 5.332542]   
  17. Row 1: [5 5.332542]  

 

可以看到,format和printf是等价的,他们只需要一个简单的格式化字符串,加上一串参数即可,每个参数对应一个格式修饰符

 

6、枚举(Enum)

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

Java代码 复制代码
  1. public class GradeTest {   
  2.     public static void main(String[] args) {   
  3.         Student stu = new Student();   
  4.         stu.setName("wasw100");   
  5.         stu.setGrade(Grade.A);   
  6.         //输出学生信息   
  7.         System.out.println(stu);   
  8.     }   
  9. }   
  10.   
  11. /**  
  12.  * 枚举:Grader 学生考试等级  
  13.  * @author wasw100  
  14.  */  
  15. enum Grade{   
  16.     A,B,C,D   
  17. }   
  18.   
  19. class Student {   
  20.     private String name;   
  21.     private Grade grade;   
  22.        
  23.     //重写toString()方法   
  24.     public String toString(){   
  25.         return "name:+"+name+"/ngrader:"+grade;   
  26.     }   
  27.   
  28.     public String getName() {   
  29.         return name;   
  30.     }   
  31.   
  32.     public void setName(String name) {   
  33.         this.name = name;   
  34.     }   
  35.   
  36.     public Grade getGrade() {   
  37.         return grade;   
  38.     }   
  39.   
  40.     public void setGrade(Grade grade) {   
  41.         this.grade = grade;   
  42.     }   
  43.   
  44. }  
Java代码 复制代码
  1. public class GradeTest {   
  2.     public static void main(String[] args) {   
  3.         Student stu = new Student();   
  4.         stu.setName("wasw100");   
  5.         stu.setGrade(Grade.A);   
  6.         //输出学生信息   
  7.         System.out.println(stu);   
  8.     }   
  9. }   
  10.   
  11. /**  
  12.  * 枚举:Grader 学生考试等级  
  13.  * @author wasw100  
  14.  */  
  15. enum Grade{   
  16.     A,B,C,D   
  17. }   
  18.   
  19. class Student {   
  20.     private String name;   
  21.     private Grade grade;   
  22.        
  23.     //重写toString()方法   
  24.     public String toString(){   
  25.         return "name:+"+name+"/ngrader:"+grade;   
  26.     }   
  27.   
  28.     public String getName() {   
  29.         return name;   
  30.     }   
  31.   
  32.     public void setName(String name) {   
  33.         this.name = name;   
  34.     }   
  35.   
  36.     public Grade getGrade() {   
  37.         return grade;   
  38.     }   
  39.   
  40.     public void setGrade(Grade grade) {   
  41.         this.grade = grade;   
  42.     }   
  43.   
  44. }  

 

7、可变长参数(varargs)

方法的参数是不固定的我们一般会使用重载或者使用数组参数。重载需要些更多写更多的方法,数组需要在使用时先声明。

可能参数是一个不错的解决方案。

下面是网上一个 唐僧 给 悟空 将佛经的例子

Java代码 复制代码
  1. public class VarargsTest {   
  2.     public void speak(String name, Object... arguments) {   
  3.         System.out.print(name+": ");   
  4.         for (Object object : arguments) {   
  5.             System.out.print(object);   
  6.         }   
  7.         System.out.println();   
  8.     }   
  9.   
  10.     public static void main(String[] args) {   
  11.         VarargsTest vt = new VarargsTest();   
  12.         vt.speak("悟空""人和妖精都是妈生的,");   
  13.         vt.speak("悟空""不同的人是人他妈生的,""妖是妖他妈生的,");   
  14.     }   
  15. }   
  16.   
  17. 运行结果:   
  18. 悟空: 人和妖精都是妈生的,   
  19. 悟空: 不同的人是人他妈生的,妖是妖他妈生的,  
Java代码 复制代码
  1. public class VarargsTest {   
  2.     public void speak(String name, Object... arguments) {   
  3.         System.out.print(name+": ");   
  4.         for (Object object : arguments) {   
  5.             System.out.print(object);   
  6.         }   
  7.         System.out.println();   
  8.     }   
  9.   
  10.     public static void main(String[] args) {   
  11.         VarargsTest vt = new VarargsTest();   
  12.         vt.speak("悟空""人和妖精都是妈生的,");   
  13.         vt.speak("悟空""不同的人是人他妈生的,""妖是妖他妈生的,");   
  14.     }   
  15. }   
  16.   
  17. 运行结果:   
  18. 悟空: 人和妖精都是妈生的,   
  19. 悟空: 不同的人是人他妈生的,妖是妖他妈生的,  

可变长参数只能作为最后一个参数。

 

 

注:本文转自http://jetway.javaeye.com/blog/491742

原创粉丝点击