Java重温学习笔记,Java5新特性

1.泛型(Generic)

泛型提供编译时类型安全检测机制,该机制允许程序员在编译时检测到非法类型。泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数。

一个泛型的基本应用:

import java.util.*;

public class MyDemo {
  public static void main(String[] args) {
      // 泛型最基本的应用,保证放到集合中的元素都是正确的类型
    List<String> strList = new ArrayList<String>();
    strList.add("this is string1");
    strList.add("this is string2");
    
    // 下面这行会出现编译错误,因为加入的对象不是String类型
    //strList.add(12345);
    
// Java5 之前,对List的遍历方式 for (int n=0; n<strList.size(); n++) { System.out.println(strList.get(n)); } // Java5 之后,可以用如下方法对List进行遍历 for (String str: strList) { System.out.println(str); } } }

更多泛型阅读,参考:

Java重温学习笔记,关于泛型

Java重温学习笔记,泛型通配符

2.For-Each循环(The For-Each Loop)

For-Each循环得加入,大大简化集合的遍历。参考如下代码:

import java.util.*;

public class MyDemo {
  public static void main(String[] args) {
      // 泛型最基本的应用,保证放到集合中的元素都是正确的类型
    List<String> strList = new ArrayList<String>();
    strList.add("this is string1");
    strList.add("this is string2");
    
    // Java5 之后可以用如下方法对List进行遍历
    for (String str: strList) {
         System.out.println(str);
    }    
  }
}

对上述代码生成的class文件进行反编译,结果如下:

import java.io.PrintStream;
import java.util.*;

public class MyDemo {

    public MyDemo() {
    }

    public static void main(String args[]) {
        ArrayList arraylist = new ArrayList();
        arraylist.add("this is string1");
        arraylist.add("this is string2");
        String s;
        for(Iterator iterator = arraylist.iterator(); iterator.hasNext(); System.out.println(s))
            s = (String)iterator.next();

    }
}

3.自动装箱/拆箱(Autoboxing/Unboxing)

装箱就是primitive类型转成对应的wrapper类型,拆箱就是反过来。毫无疑问,自动装箱/拆箱大大简化基本数据类型和它们包装类的使用。

参考如下代码:

import java.util.*;

public class MyDemo {
    public static void main(String args[]) {
        // 自动装箱
        Integer score = 53;
        System.out.println(score);

        // 自动拆箱
        int newScore = score + 7;
        System.out.println(newScore);
    }
}

对上述代码生成的class文件进行反编译,结果如下:

import java.io.PrintStream;

public class MyDemo {

    public MyDemo()
    {
    }

    public static void main(String args[]) {
        Integer integer = Integer.valueOf(53);
        System.out.println(integer);
        int i = integer.intValue() + 7;
        System.out.println(i);
    }
}

从反编译后的代码可以看出,自动装箱就是编译器帮我们调用了valueOf方法,自动拆箱就是帮我们调用了intValue方法。

4.枚举(Enums)

请看下面的代码:

import java.io.PrintStream;

enum Color {
   Red,
   White,
   Blue
 }

public class MyDemo {
    public static void main(String args[]) {
        Color myColor = Color.Red;
        switch (myColor) {
            case Red:
                System.out.println("Red");
                break;
            case White:
                System.out.println("White");
                break;
            case Blue:
                System.out.println("Blue");
                break;
        }
        
        // 对枚举类遍历
        for (Color c : Color.values())
            System.out.println(c);
   }
}

更多枚举阅读,参考:

Java重温学习笔记,迭代枚举元素

5.可变参数(Varargs)

请看下面的代码:

import java.io.PrintStream;

public class MyDemo {
    // 不可以定义如下同名方法,因为int... myValue,其实等同于int[] myValue。否则出现编译错误:
    // 错误: 无法在MyDemo中同时声明sumValue(int...)和sumValue(int[])
    //public static int sumValue(int[] myValue) {
    //}

    public static int sumValue(int... myValue) {
        int result = 0;
        for (int i = 0; i < myValue.length; i++) {
            result += myValue[i];  // 从这儿也可以看出,myValue其实是一个数组。
        }
        return result;
    }
  
    public static void main(String args[]) {
        System.out.println(sumValue(1, 2, 3));
        System.out.println(sumValue(1, 2, 3, 4));
   }
}

6.静态导入(Static Imports)

以前,使用其它类包的静态方法或变量,我们必须给出提供这个方法的类。使用静态导入可以使之简化。以前的方法:

public class MyDemo {
    public static void main(String args[]) {
        System.out.println(Math.max(2, 5));
        System.out.println(Math.PI);
   }
}

现在的方法:

import static java.lang.Math.PI;
import static java.lang.Math.max;

public class MyDemo {
    public static void main(String args[]) {
        System.out.println(max(2, 5));
        System.out.println(PI);
   }
}

说实话,本人不赞成使用静态导入,因为它使代码的可读性降低。

7.元数据(Annotations)

注解(也被称为元数据):为我们在代码中添加信息提供了一种形式化的方法,使我们可以在稍后某个时刻非常方便的使用这些数据。
javaSE内置了3种标准注解:

   a. @SuppressWarnings
    该注解的作用是阻止编译器发出某些警告信息。
    它可以有以下参数:
    deprecation :过时的类或方法警告。
    unchecked:执行了未检查的转换时警告。
    fallthrough:当Switch程序块直接通往下一种情况而没有Break时的警告。
    path:在类路径、源文件路径等中有不存在的路径时的警告。
    serial:当在可序列化的类上缺少serialVersionUID定义时的警告。
    finally:任何finally子句不能完成时的警告。
    all:关于以上所有情况的警告。
  b.@Deprecated
  该注解的作用是标记某个过时的类或方法。
  c. @Override
  该注解用在方法前面,用来标识该方法是重写父类的某个方法。

本文参考:

https://www.cnblogs.com/strongmore/p/13282777.html

https://www.cnblogs.com/jtlgb/p/6366087.html

https://docs.oracle.com/javase/7/docs/technotes/guides/language/enhancements.html#javase7

原文地址:https://www.cnblogs.com/nayitian/p/14911041.html