Summary of new features in JDK 1.5 - 1.8 versions

1.6 Using the Compiler API

    The Compiler API (JSR 199) of JDK6 is used to dynamically compile Java source files. Combined with the reflection function, the Compiler API can dynamically generate Java code and compile and execute these codes, which is a bit characteristic of dynamic languages.

    The Compiler API provides a richer way to do dynamic compilation through a set of easy-to-use standard APIs, and it is cross-platform.

 

1.7

1. Strings can be used in switch

    

2. "<>" the use of this thing

ListtempList = new ArrayList<>(); That is, the generic instantiation type is automatically inferred.

 

3. Custom auto-close class

 

interface AutoCloseable
As long as this interface is implemented, the close method is automatically called when the object of this class is destroyed. You can close the resources you want to close in the close method.

 

4. Enhanced support for Java collections (Collections)

  1. List<String> list=["item"]; //向List集合中添加元素

  2.      String item=list[0]; //从List集合中获取元素

  3.      Set<String> set={"item"}; //向Set集合对象中添加元素

  4.      Map<String,Integer> map={"key":1}; //向Map集合中添加对象

  5.      int value=map["key"]; //从Map集合中获取对象

 

5. Values ​​can be underlined

int one_million = 1_000_000;

6. Support binary literals

int binary = 0b1001_1001;

7. In try catch exception capture, a catch can write multiple exception types, separated by "|"

try {

......

} catch(ClassNotFoundException|SQLException ex) {

  ex.printStackTrace();

}

8. Before jdk7, you must use try{}finally{} to use resources in try and close resources in finally, regardless of whether the code in try exits normally or abnormally. After jdk7, you can close the resource without writing a finally statement, as long as you define the resource to be used inside the brackets of try()

  1. public class FileCopyJDK7 {

  2.   public static void main(String[] args) {

  3.      try (BufferedReader in  = new BufferedReader(new FileReader("in.txt"));

  4.           BufferedWriter out = new BufferedWriter(new FileWriter("out.txt"))) {

  5.         int charRead;

  6.         while ((charRead = in.read()) != -1) {

  7.            System.out.printf("%c ", (char)charRead);

  8.            out.write(charRead);

  9.         }

  10.      } catch (IOException ex) {

  11.         ex.printStackTrace();

  12.      }

  13.   }

  14. }

 

1.8

1. The default method of the interface

  1. interface Formula {

  2.    double calculate(int a);

  3.    default double sqrt(int a) {

  4.        return Math.sqrt(a);

  5.    }

  6. }

 

2. Lambda expressions

3. Functional interface

.....

https://mp.weixin.qq.com/s/-mG6XsXLCXTnL-efV3mX4Q

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326153393&siteId=291194637