序
本文主要讲述一下Java19的新特性
版本号
代码语言:txt复制java -version
openjdk version "19" 2022-09-20
OpenJDK Runtime Environment (build 19 36-2238)
OpenJDK 64-Bit Server VM (build 19 36-2238, mixed mode, sharing)
从version信息可以看出是build 19 36
特性列表
JEP 405: Record Patterns (Preview)
instanceof的模式匹配在JDK14作为preview,在JDK15作为第二轮的preview,在JDK16JEP 394: Pattern Matching for instanceof转正
switch模式匹配在JDK17的JEP 406: Pattern Matching for switch (Preview)引入作为preview版本,在JDK18的JEP 420: Pattern Matching for switch (Second Preview)作为第二轮的preview
针对record类型,引入instance of可以这么写
代码语言:txt复制record Point(int x, int y) {}
static void printSum(Object o) {
if (o instanceof Point p) {
int x = p.x();
int y = p.y();
System.out.println(x y);
}
}
现在可以这么写
代码语言:txt复制record Point(int x, int y) {}
void printSum(Object o) {
if (o instanceof Point(int x, int y)) {
System.out.println(x y);
}
}
比较复杂的例子:
代码语言:txt复制record Point(int x, int y) {}
enum Color { RED, GREEN, BLUE }
record ColoredPoint(Point p, Color c) {}
record Rectangle(ColoredPoint upperLeft, ColoredPoint lowerRight) {}
Rectangle r = new Rectangle(new ColoredPoint(new Point(x1, y1), c1),
new ColoredPoint(new Point(x2, y2), c2));
static void printXCoordOfUpperLeftPointWithPatterns(Rectangle r) {
if (r instanceof Rectangle(ColoredPoint(Point(var x, var y), var c),
var lr)) {
System.out.println("Upper-left corner: " x);
}
}
如果是泛型record的话:
代码语言:txt复制record Box<T>(T t) {}
static void test1(Box<Object> bo) {
if (bo instanceof Box<Object>(String s)) {
System.out.println("String " s);
}
}
static void test2(Box<Object> bo) {
if (bo instanceof Box<String>(var s)) {
System.out.println("String " s);
}
}
JEP 422: Linux/RISC-V Port
RISC-V是一个基于精简指令集(RISC)原则的开源指令集架构(ISA),这个JEP则移植JDK到RISC-V上
JEP 424: Foreign Function & Memory API (Preview)
Foreign Function & Memory (FFM) API包含了两个incubating API
JDK14的JEP 370: Foreign-Memory Access API (Incubator)引入了Foreign-Memory Access API作为incubator
JDK15的JEP 383: Foreign-Memory Access API (Second Incubator)Foreign-Memory Access API作为第二轮incubator
JDK16的JEP 393: Foreign-Memory Access API (Third Incubator)作为第三轮,它引入了Foreign Linker API (JEP 389)
FFM API在JDK 17的JEP 412: Foreign Function & Memory API (Incubator)作为incubator引入
FFM API在JDK 18的JEP 419: Foreign Function & Memory API (Second Incubator)作为第二轮incubator
JDK19的这个JEP则将FFM API作为preview API
使用示例
代码语言:txt复制// 1. Find foreign function on the C library path
Linker linker = Linker.nativeLinker();
SymbolLookup stdlib = linker.defaultLookup();
MethodHandle radixSort = linker.downcallHandle(
stdlib.lookup("radixsort"), ...);
// 2. Allocate on-heap memory to store four strings
String[] javaStrings = { "mouse", "cat", "dog", "car" };
// 3. Allocate off-heap memory to store four pointers
SegmentAllocator allocator = SegmentAllocator.implicitAllocator();
MemorySegment offHeap = allocator.allocateArray(ValueLayout.ADDRESS, javaStrings.length);
// 4. Copy the strings from on-heap to off-heap
for (int i = 0; i < javaStrings.length; i ) {
// Allocate a string off-heap, then store a pointer to it
MemorySegment cString = allocator.allocateUtf8String(javaStrings[i]);
offHeap.setAtIndex(ValueLayout.ADDRESS, i, cString);
}
// 5. Sort the off-heap data by calling the foreign function
radixSort.invoke(offHeap, javaStrings.length, MemoryAddress.NULL, '