`
地方疙瘩人
  • 浏览: 36952 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

JAVA5新特性

 
阅读更多
引用
java5就已经引入静态导入,很少见人用,很多人都没听说过,所以总结下java5-java8的新特性。

1、泛型 Generics:
        引用泛型之后,允许指定集合里元素的类型,免去了强制类型转换,并且能在编译时刻进行类型检查的好处。

        Parameterized Type作为参数和返回值,Generic是vararg、annotation、enumeration、collection的基石。



        A、类型安全
           抛弃List、Map,使用List<T>、Map<K,V>给它们添加元素或者使用Iterator<T>遍历时,编译期就可以给你检查出类型错误

        B、方法参数和返回值加上了Type

        抛弃List、Map,使用List<T>、Map<K,V>

          C、不需要类型转换


          List<String> list=new ArrayList<String>();

       String str=list.get(i);
         D、类型通配符“?”


       假设一个打印List<T>中元素的方法printList,我们希望任何类型T的List<T>都可以被打印:

    
public void printList(List<?> list,PrintStream out)throws IOException{  
for(Iterator<?> i=list.iterator();i.hasNext();){  
System.out.println(i.next.toString());  
}  
  
}  


如果通配符?让我们的参数类型过于广泛,我们可以把List<?>、Iterator<?> 修改为

      List<? Extends Number>、Iterator<? Extends Number>限制一下它。

2、枚举类型 Enumeration:

3、自动装箱拆箱(自动类型包装和解包)autoboxing & unboxing:

  简单的说是类型自动转换。

   自动装包:基本类型自动转为包装类(int ——Integer)

   自动拆包:包装类自动转为基本类型(Integer——int)

4、可变参数varargs(varargs number of arguments)

  参数类型相同时,把重载函数合并到一起了。

  如:public void test(object... objs){

for(Object obj:objs){

System.out.println(obj);

}

}

5、Annotations 它是java中的metadata(注释)

     A、Tiger中预定义的三种标准annotation

           a 、Override

           指出某个method覆盖了superclass 的method当你要覆盖的方法名拼写错时编译不通过

           b、Deprecated

           指出某个method或element类型的使用是被阻止的,子类将不能覆盖该方法

           c、SupressWarnings

           关闭class、method、field、variable 初始化的编译期警告,比如:List没有使用 Generic,则@SuppressWarnings("unchecked")去掉编译期警告。

     B、自定义annotation

          public @interface Marked{}

     C、meta-annotation

         或者说annotation的annotation

         四种标准的meta-annotation全部定义在java.lang.annotaion包中:
          a, Target
           指定所定义的annotation可以用在哪些程序单元上
           如果Target没有指定,则表示该annotation可以使用在任意程序单元上
@Retention(RetentionPolicy.RUNTIME)    
@Target({ ElementType.FIELD, ElementType.METHOD })    
public @interface RejectEmpty {    
   /** hint title used in error message */    
 String value() default "";    
}    
   
@Retention(RetentionPolicy.RUNTIME)    
@Target( { ElementType.FIELD, ElementType.METHOD })    
public @interface AcceptInt {    
 int min() default Integer.MIN_VALUE;    
int max() default Integer.MAX_VALUE;    
  String hint() default "";    
}  

b, Retention
     指出Java编译期如何对待annotation
      annotation可以被编译期丢掉,或者保留在编译过的class文件中
        在annotation被保留时,它也指定是否会在JVM加载class时读取该annotation
@Retention(RetentionPolicy.SOURCE)  // Annotation会被编译期丢弃    
public @interface TODO1 {}    
 @Retention(RetentionPolicy.CLASS)   // Annotation保留在class文件中,但会被JVM忽略    
 public @interface TODO2 {}    
@Retention(RetentionPolicy.RUNTIME) // Annotation保留在class文件中且会被JVM读取    
 public @interface TODO3 {}    

c, Documented
       指出被定义的annotation被视为所熟悉的程序单元的公开API之一
        被@Documented标注的annotation会在javadoc中显示,这在annotation对它标注的元素被客户端使用时有影响时起作用
       d, Inherited
       该meta-annotation应用于目标为class的annotation类型上,被此annotattion标注的class会自动继承父类的annotation
    D, Annotation的反射
    我们发现java.lang.Class有许多与Annotation的反射相关的方法,如getAnnotations、isAnnotationpresent
    我们可以利用Annotation反射来做许多事情,比如自定义Annotation来做Model对象验证

 @Retention(RetentionPolicy.RUNTIME)    
 @Target({ ElementType.FIELD, ElementType.METHOD })    
public @interface RejectEmpty {    
   /** hint title used in error message */    
  String value() default "";    
 }    
     
 @Retention(RetentionPolicy.RUNTIME)    
@Target( { ElementType.FIELD, ElementType.METHOD })    
 public @interface AcceptInt {    
     int min() default Integer.MIN_VALUE;    
    int max() default Integer.MAX_VALUE;    
 String hint() default "";    
} 

使用@RejectEmpty和@AcceptInt标注我们的Model的field,然后利用反射来做Model验证



6、新的迭代语句(for(int n:numbers))

7、静态导入(import static )

8、新的格式化方法(java.util.Formatter)

formatter.format("Remaining account balance: $%.2f", balance);

9、新的线程模型和并发库Thread Framework

HashMap的替代者ConcurrentHashMap和ArrayList的替代者CopyOnWriteArrayList
在大并发量读取时采用java.util.concurrent包里的一些类会让大家满意BlockingQueue、Callable、Executor、Semaphore...
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics