Differences and new features of JDK 8 and JDK 17

background introduction

Java is a widely used programming language with a rich ecosystem and a large number of developers. The official implementation of Java is Oracle's JDK (Java Development Kit), the latest version is JDK 17, and JDK 8 is a long-term support version (LTS).

Released in 2014, JDK 8 introduced many important new features, such as Lambda expressions, Stream API, and new date and time API, etc. And JDK 17 will be released in 2021, which has improved and improved performance, security and language features.

This article will introduce the differences and new features between JDK 8 and JDK 17.

New features in JDK 8

Here are some new features of JDK 8:

  1. Lambda expression
  2. Stream API
  3. New datetime API
  4. interface default method
  5. method reference
  6. Type Annotations
  7. Better Nashorn Engine
  8. parallel array

These new features have greatly improved the programming efficiency and code readability of Java, especially the Lambda expression and Stream API, which allow developers to write efficient functional code more conveniently.

New features in JDK 17

Here are some new features of JDK 17:

  1. Switch expression improvements
  2. Class-based records (Records)
  3. Sealed Class
  4. Pattern Matching for instanceof
  5. Oracle memory allocation
  6. Enhanced exception handling
  7. Enhanced security
  8. enhanced performance
  9. Improved Garbage Collector
  10. new language features

The new features of JDK 17 cover many aspects of Java development, including language features, performance, security, garbage collector, etc. Among them, class-based records (Records) and Sealed Class are very important language features, which can make Java code more concise and more readable. At the same time, predictive memory allocation and an improved garbage collector also improve the performance and reliability of Java programs.


Code comparison example of JDK 8 and JDK 17

Here are some simple examples showing some of the differences between JDK8 and JDK17. Of course, in actual development, there are many other changes and improvements that need to be analyzed in combination with specific application scenarios

Lambda expression

// JDK8
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
list.forEach(n -> System.out.println(n));

// JDK17
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
list.forEach((var n) -> System.out.println(n));

Pattern Matching for instanceof

// JDK8
if (obj instanceof String) {
    
    
    String str = (String) obj;
    System.out.println(str.toUpperCase());
} else {
    
    
    System.out.println(obj);
}

// JDK17
if (obj instanceof String str) {
    
    
    System.out.println(str.toUpperCase());
} else {
    
    
    System.out.println(obj);
}

new library

// JDK8
// 没有Java Flight Recorder库
// 没有ZGC垃圾回收器

// JDK17
// 使用Java Flight Recorder记录CPU时间
JFR.enable();
// 使用ZGC垃圾回收器
System.setProperty("jdk.garbageCollector", "Z");

New set operation API

JDK8 introduces the Stream API, which makes operations on collections more concise and convenient. On this basis, JDK17 adds some new APIs, such as toList()and toSet(), which make it easier to convert Stream to List or Set.

// JDK8
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
int sum = list.stream().filter(n -> n % 2 == 0).mapToInt(Integer::intValue).sum();

// JDK17
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> evenList = list.stream().filter(n -> n % 2 == 0).toList();
Set<Integer> oddSet = list.stream().filter(n -> n % 2 != 0).toSet();

Improved Switch statement

JDK17 introduces a new Switch statement, which supports the use of expressions as conditions, and also supports the use of Lambda expressions as branch statements.

// JDK8
String dayOfWeek = "MON";
switch (dayOfWeek) {
    
    
    case "MON":
    case "TUE":
    case "WED":
    case "THU":
    case "FRI":
        System.out.println("Weekday");
        break;
    case "SAT":
    case "SUN":
        System.out.println("Weekend");
        break;
    default:
        System.out.println("Invalid day");
}

// JDK17
String dayOfWeek = "MON";
String dayType = switch (dayOfWeek) {
    
    
    case "MON", "TUE", "WED", "THU", "FRI" -> "Weekday";
    case "SAT", "SUN" -> "Weekend";
    default -> {
    
    
        System.out.println("Invalid day");
        yield "Invalid day";
    }
};

New HTTP Client API

JDK11 introduces a new HTTP Client API, which makes the processing of HTTP requests easier and more convenient.

// JDK8
// 使用第三方HTTP库

// JDK17
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
    .uri(URI.create("https://www.example.com/"))
    .build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());

Summarize

Both JDK 8 and JDK 17 are important releases for Java development, and they both introduce many important new features and improvements. While JDK 8 is a long-term support release, new features and improvements in JDK 17 make it more suitable for modern Java development. For Java developers, mastering the new features and improvements of JDK 17 will help improve development efficiency and code quality.

In actual development, developers can choose the appropriate JDK version according to project requirements and development scenarios. For some old projects, you may need to use JDK 8 to ensure stability and compatibility; for some new projects, you can use JDK 17 to take advantage of the latest technologies and features to improve code readability and performance.

In short, the continuous update and improvement of JDK has brought more choices and opportunities to Java developers, making Java continue to develop and improve. We expect that the new version of JDK will continue to promote the development and innovation of Java technology in the future.

download link

JDK8 download address

Oracle official website download link: Java SE Development Kit 8 Downloads
OpenJDK community website download link

JDK17 download address

Oracle official website download link: Java SE Development Kit 17 Downloads
OpenJDK community website download link

Guess you like

Origin blog.csdn.net/qq_37686995/article/details/129949487