JAVA5 新特性

JDK1.5引入很多新的特性,在进行开发时可以借助这些新的特性来更方便简洁的开发,缩短开发时间,以下是列举的一些新的特性,具体使用可以参考帮助文档,在此仅以点到为止,不做具体详细阐明

1. 数组工具Arrays,改类是一个工具类,提供很多数组操作工具方法

2. 队列Queue,提供各种实现方式,包括阻塞和非阻塞等

3. StringBuilder 在线程安全不是一个问题的情况下,可以完全替换掉StringBuffer,接口方法一致,非线程安全

4. 模板方法支持,例如List<Integer> list = new LinkedList<Integer>( );
5. Enum枚举类型支持,switch支持枚举类型,如下:

    声明一个枚举类型级别:

    public enum Grade {
    A, B, C, D, E
    };

    public static void testEnum() throws Exception {
        Grade grade = Grade.A;
        switch (grade) {
        case A:
            System.out.println("A...");
            break;
        case B:
            System.out.println("B...");
            break;
        default:
            System.out.println("DEFAULT...");
            break;
        }
    }

   

6. 基本类型数据与包装对象自动转换,即自动装箱,拆箱,如下

   public static void testAutoBox() throws Exception{
        int intI = 10;
        Integer counter = intI; // 裝箱
        int intJ = counter;     // 拆箱
        while(true){
            System.out.println("Integer: "+counter++);
            if(counter>20)
                break;
        }
    }

    对于JDK1.5之前版本,上述代码会产生编译错误,现在对于int与Integer直接可以自动转换,不需要手动包装转换,还有boolean与   Boolean等等.....

 

7. 支持方法参数可变,不确定参数个数

    例如:

   public static int max(int first, int... rest) {
        int max = first;
        for (int i : rest) {
            if (i > max)
                max = i;
        }
        return max;
    }

    对于可变参数后台实际为一个对应的数组

    public static void testArguments() throws Exception {
        System.out.println(MyTest.max(2, 3, 4, 6));
        System.out.println(MyTest.max(2, 3, 4, 6, 9));
        System.out.println(MyTest.max(2, 3, 4, 6, 12, 15));
    }

 

8. 注解Annotations,JDK1.5开始引入注解,目前很多开源框架引入注解来替换大量的配置文件,具体可以参考JDK官方文档,在此不做  说明

 

9. for 语句,例如,更加简洁

    public static void testForStatement() throws Exception{
        List<String> wordList = new LinkedList<String>( );
        // Assign some words
        for (int i=0; i<10; i++) {
          wordList.add("word " + (i+1) + ": '" + i + "'");
        }
        for(String word: wordList){
            System.out.println(word);
        }
    }

   

10. 静态导入

    例如:

    import static java.lang.System.out;

    导入System.out后,在其代码中可以省略System,个人不建议如此使用

    public static void testStaticImport() throws Exception{
        out.println("helleo!");
        System.out.println("hello!");
    }

 

11. 线程池,线程同步处理更方便支持,java.util.concurrent

   改包提供线程处理的工具类,同时提供线程安全的容器支持

 

更多细节可以参考源代码,看看究竟是如何实现的,可以根据自己需要进行个性化包装........

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值