[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/UserXSX/JavaGuide/main/docs/java/new-features/java20.md [Back]  [Original]

---
title: Java 20 
description:  JDK 20 
category: Java
tag:
  - Java
head:
  - - meta
    - name: keywords
      content: Java 20,JDK20,,,,JEP
---

JDK 20  2023  3  21 

 LTS  2023  9  JDK 21

JDK 20  7 

- [JEP 429: Scoped Values](https://openjdk.org/jeps/429)
- [JEP 432: Record Patterns](https://openjdk.org/jeps/432)
- [JEP 433: Pattern Matching for switchswitch ](https://openjdk.org/jeps/433)
- [JEP 434: Foreign Function & Memory API API](https://openjdk.org/jeps/434)
- [JEP 436: Virtual Threads](https://openjdk.org/jeps/436)
- [JEP 437: Structured Concurrency](https://openjdk.org/jeps/437)
- [JEP 438: Vector API API](https://openjdk.org/jeps/438)

 JDK 8  JDK 25 

![ JDK 8  JDK 25 ](https://oss.javaguide.cn/github/javaguide/java/new-features/jdk8~jdk24.png)

## JEP 429: Scoped Values

Scoped Values

```java
final static ScopedValue V = new ScopedValue();

// In some method
ScopedValue.where(V, )
           .run(() -> { ... V.get() ... call methods ... });

// In a method called directly or indirectly from the lambda expression
... V.get() ...
```



[](https://www.happycoders.eu/java/scoped-values/)

## JEP 432: Record Patterns

Record Patterns  record Record Class

 instanceof  switch 

 instanceof 



```java
record Shape(String type, long unit){}
```



```java
Shape circle = new Shape("Circle", 10);
if (circle instanceof Shape shape) {

  System.out.println("Area of " + shape.type() + " is : " + Math.PI * Math.pow(shape.unit(), 2));
}
```



```java
Shape circle = new Shape("Circle", 10);
if (circle instanceof Shape(String type, long unit)) {
  System.out.println("Area of " + type + " is : " + Math.PI * Math.pow(unit, 2));
}
```

 switch 



```java
interface Shape {}
record Circle(double radius) implements Shape { }
record Square(double side) implements Shape { }
record Rectangle(double length, double width) implements Shape { }
```



```java
Shape shape = new Circle(10);
switch (shape) {
    case Circle c:
        System.out.println("The shape is Circle with area: " + Math.PI * c.radius() * c.radius());
        break;

    case Square s:
        System.out.println("The shape is Square with area: " + s.side() * s.side());
        break;

    case Rectangle r:
        System.out.println("The shape is Rectangle with area: " + r.length() * r.width());
        break;

    default:
        System.out.println("Unknown Shape");
        break;
}
```



```java
Shape shape = new Circle(10);
switch(shape) {

  case Circle(double radius):
    System.out.println("The shape is Circle with area: " + Math.PI * radius * radius);
    break;

  case Square(double side):
    System.out.println("The shape is Square with area: " + side * side);
    break;

  case Rectangle(double length, double width):
    System.out.println("The shape is Rectangle with area: " + length * width);
    break;

  default:
    System.out.println("Unknown Shape");
    break;
}
```

 `null`  `NullPointerException`

 Java 19   [JEP 405](https://openjdk.org/jeps/405) JDK 20  [JEP 432](https://openjdk.org/jeps/432) 

- 
- `for`
- 

**** [JDK16](./java16.md) 

## JEP 433: Pattern Matching for switchswitch 

 `instanceof`  `switch` 

`instanceof` 

```java
// Old code
if (o instanceof String) {
    String s = (String)o;
    ... use s ...
}

// New code
if (o instanceof String s) {
    ... use s ...
}
```

`switch` 

```java
// Old code
static String formatter(Object o) {
    String formatted = "unknown";
    if (o instanceof Integer i) {
        formatted = String.format("int %d", i);
    } else if (o instanceof Long l) {
        formatted = String.format("long %d", l);
    } else if (o instanceof Double d) {
        formatted = String.format("double %f", d);
    } else if (o instanceof String s) {
        formatted = String.format("String %s", s);
    }
    return formatted;
}

// New code
static String formatterPatternSwitch(Object o) {
    return switch (o) {
        case Integer i -> String.format("int %d", i);
        case Long l    -> String.format("long %d", l);
        case Double d  -> String.format("double %f", d);
        case String s  -> String.format("String %s", s);
        default        -> o.toString();
    };
}
```

`switch`  Java17Java18Java19 Java20 

## JEP 434: Foreign Function & Memory API API

Java  API  Java  JVM  JVM  API  Java  JNI 

 API  Java 17  [JEP 412](https://openjdk.java.net/jeps/412) Java 18 [JEP 419](https://openjdk.org/jeps/419) Java 19  [JEP 424](https://openjdk.org/jeps/424) 

JDK 20  [JEP 434](https://openjdk.org/jeps/434) 

- `MemorySegment`  `MemoryAddress` 
-  `MemoryLayout` 
- `MemorySession``Arena``SegmentScope`

 [Java 19 ](./java19.md)  API

## JEP 436: Virtual Threads

Virtual Thread JDK  OS Lightweight ProcessLWP JVM 

`java.lang.Thread` JVM 

[How to Use Java 19 Virtual Threads](https://medium.com/javarevisited/how-to-use-java-19-virtual-threads-c16a32bad5f7)

![](https://oss.javaguide.cn/github/javaguide/java/new-features/virtual-threads-platform-threads-kernel-threads-relationship.png)

 Windows  Linux Java Solaris HotSpot VM  Solaris  R : [JVM ](https://www.zhihu.com/question/23096638/answer/29617153)



 Go  GoroutineErlang 

 Java 19  

Java 

- [](https://javaguide.cn/java/concurrent/virtual-thread.html)
- [Java19  GA](https://mp.weixin.qq.com/s/yyApBXxpXxVwttr01Hld6Q)
- [ - VirtualThread ](https://www.cnblogs.com/throwable/p/16758997.html)

 Java 19 [JEP 425](https://openjdk.org/jeps/425)JDK 20 



```java
// 1 Thread.ofVirtual() 
Runnable fn = () -> {
  // your code here
};

Thread thread = Thread.ofVirtual(fn)
                      .start();

// 2 Thread.startVirtualThread() 
Thread thread = Thread.startVirtualThread(() -> {
  // your code here
});

// 3 Executors.newVirtualThreadPerTaskExecutor() 
var executorService = Executors.newVirtualThreadPerTaskExecutor();

executorService.submit(() -> {
  // your code here
});

class CustomThread implements Runnable {
  @Override
  public void run() {
    System.out.println("CustomThread run");
  }
}

//4 ThreadFactory 
CustomThread customThread = new CustomThread();
// 
ThreadFactory factory = Thread.ofVirtual().factory();
// 
Thread thread = factory.newThread(customThread);
// 
thread.start();
```

 4  `Thread` 

## JEP 437: Structured Concurrency

Java 19  API `java.util.concurrent`



 API [`StructuredTaskScope`](https://download.java.net/java/early_access/loom/docs/api/jdk.incubator.concurrent/jdk/incubator/concurrent/StructuredTaskScope.html)`StructuredTaskScope` 

`StructuredTaskScope` 

```java
    try (var scope = new StructuredTaskScope()) {
        // fork
        Future future1 = scope.fork(task1);
        Future future2 = scope.fork(task2);
        // 
        scope.join();
        // 
        ... process results/exceptions ...
    } // close
```

 JDK 

JDK 20 `StructuredTaskScope` [JEP 429](https://openjdk.org/jeps/429)

## JEP 438: Vector API API

 API  CPU 

 API 

Vector API  [JEP 338](https://openjdk.java.net/jeps/338) [ API](http://openjdk.java.net/jeps/11) Java 16  [JEP 414](https://openjdk.java.net/jeps/414)  Java 17  [JEP 417](https://openjdk.java.net/jeps/417)  Java 18  [JEP 426](https://openjdk.java.net/jeps/426)  Java 19 

Java20  API  [JEP 438](https://openjdk.org/jeps/438)



Web Proxy Viewer  |  New URL  |  Original Page