Share
Embed
Java 24まとめ / Java 24 summary
2025年4月11日に開催のJavaコミュ@福岡での登壇資料です。
https://javaq.connpass.com/event/348733/
Naoki Kishida
April 11, 2025
Transcript
30 slides
-
2 Java 24 • Released March 18th • Non LTS(Long
Term Support) • next LTS will be Java 25 on Sept. • 24JEPs -
JEPs • Language • 488: Primitive Types in Patterns, instanceof,
and switch (Second Preview) • 492: Flexible Constructor Bodies (Third Preview) • 494: Module Import Declarations (Second Preview) • 495: Simple Source Files and Instance Main Methods (Fourth Preview) • API • 472: Prepare to Restrict the Use of JNI • 484: Class-File API • 485: Stream Gatherers • 487: Scoped Values (Fourth Preview) • 489: Vector API (Ninth Incubator) • 498: Warn upon Use of Memory-Access Methods in sun.misc.Unsafe • 499: Structured Concurrency (Fourth Preview) • Tool • 493: Linking Run-Time Images without JMODs • JVM • 404: Generational Shenandoah (Experimental) • 450: Compact Object Headers (Experimental) • 475: Late Barrier Expansion for G1 • 479: Remove the Windows 32-bit x86 Port • 483: Ahead-of-Time Class Loading & Linking • 490: ZGC: Remove the Non-Generational Mode • 491: Synchronize Virtual Threads without Pinning • Security • 478: Key Derivation Function API (Preview) • 486: Permanently Disable the Security Manager • 496: Quantum-Resistant Module-Lattice-Based Key Encapsulation Mechanism • 497: Quantum-Resistant Module-Lattice-Based Digital Signature Algorithm • JDK • 501: Deprecate the 32-bit x86 Port for Removal -
Tool & JDK • Small changes • jar command option
• -dir, -C to specify dir, -keep-old-files to suppress overwrite • java some command option removed • -verbosegc, -noclassgc, -verify, -verifyremote, -ss, -ms, -mx • 493: Linking Run-Time Images without JMODs • 501: Deprecate the 32-bit x86 Port for Removal -
Language • 488: Primitive Types in Patterns, instanceof, and switch
(Second Preview) • 492: Flexible Constructor Bodies (Third Preview) • 494: Module Import Declarations (Second Preview) • 495: Simple Source Files and Instance Main Methods (Fourth Preview) -
primitive types in pattern(preview) • primitive types are available in
patten matching such as instanceof or switch • will not be standard in Java 25 • range check for primitive • like if expression -
492: Flexible Constructor Bodies • `this`を使わないステートメントを`super`呼び出しの前に書ける • will be standard
in Java 25 class Foo { Foo(List l1, List l2) { } } class Bar extends Foo { Bar() { // need same list? super(List.of("abc", "def"), List.of("abc", "def")); } } class Bar extends Foo { // need another constructor? private Bar(List l) { super(l, l); } Bar() { this(List.of("abc", "def")); } } class Bar extends Foo { Bar() { // flexible constructor body var param = List.of("abc", "def"); super(param, param); } } 同じリストをふたつの引数に渡すような処理が書きにくかった in Java 24 in Java 21 -
Module Import Declarations(2nd preview) • import module java.base covers almost
all • transitively require dependent modules • import module java.se covers all Java SE API • will be standard in Java 25 import module java.base; public class Main { public static void main(Stiring[] args) { var data = List.of(“apple”, “grape”); data.forEach(IO::println); } } -
Simple Source File and instance main method(4th prev.) • Java
hello world will be more simple • currently we need to know many keywords, class, static, public,,, • main method can be package private, protected • main method can be instance method • main method does not require any argument • no class needed! • will be standard as Compact Source File and... in Java 25 void main() { System.out.println(“Hello world!”); } -
java.util.IO(preview) • since println is defined in IO class, System.out
is not required import module java.base; import static java.lang.IO.*; public class Main { public static void main(Stiring[] args) { var data = List.of(“apple”, “grape”); for (var str : data) { println(str); } } } -
Simple sample code • when omit the class definition, java.base
and java.util.IO will be imported automatically • (in java 25, IO will not be imported automatically) -
API • Small changes • 472: Prepare to Restrict the
Use of JNI • 484: Class-File API • 485: Stream Gatherers • 487: Scoped Values (Fourth Preview) • 489: Vector API (Ninth Incubator) • 498: Warn upon Use of Memory-Access Methods in sun.misc.Unsafe • 499: Structured Concurrency (Fourth Preview) -
Small changes • Reader.of(String) • Unicode 16 • External Specifications
in Document • https://docs.oracle.com/en/java/javase/24/docs/api/external-specs.html -
Prepare to Restrict the Use of JNI • Restrict native
access as same as FFM • java –enable-native-access=ALL-UNNAMED • --illegal-native-access=allow / warn / deny -
Stream Gatherers • Streamでは、他のデータを参照する処理ができなかった • スライディングウィンドウなど • Stream Gathererで可能に •
Gatherersは`collect`に対するCollectorsのように典型処理を まとめている jshell> IntStream.range(0, 10).boxed() .gather(Gatherers.windowFixed(3)) .toList() $1 ==> [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]] jshell> IntStream.range(0, 10).boxed() .gather(Gatherers.windowSliding(3)) .toList() $2 ==> [[0, 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7], [6, 7, 8], [7, 8, 9]] windowFixed windowSliding -
Class-File API • API for parsing, modifying, and generating Java
class file • external library such as ASM and Javassist • new feature support will be available after the new feature is released • the new feature cannot be used inside Java on development -
Vector API(9th Incubator) • invoke SIMD instructions like AVX from
Java • planned to depend on the value class of Project Valhalla • stay an incubator until the related JEP is released -
JVM • 404: Generational Shenandoah (Experimental) • 450: Compact Object
Headers (Experimental) • 475: Late Barrier Expansion for G1 • 479: Remove the Windows 32-bit x86 Port • 483: Ahead-of-Time Class Loading & Linking • 490: ZGC: Remove the Non-Generational Mode • 491: Synchronize Virtual Threads without Pinning -
450: Compact Object Headers (Exper) • オブジェクトヘッダーに現状は12-16バイト使っているのを8バイ トに削減 • 今後、デフォルト有効にしたあと現在のヘッダー用のコードを削
除する方向 • メリット • ヒープサイズが減る • メモリ局所性があがる -
479: Remove the Windows 32-bit x86 Port • Windows x86
32-bit向けのコードを削除 -
483: Ahead-of-Time Class Loading & Linking • クラスを読み込んだ状態を保存して使い回すことでJVMの起動や ピークパフォーマンスまでの時間を短縮 •
Spring BootはDIが重いのであまり効果がない・・・ • CRaCだとメモリ状態を保持するので有効 • Linuxの仕組みを使うのでLinuxのみ -
490: ZGC: Remove the Non-Generational Mode • Java 21でZGCに世代別アルゴリズムが導入 •
Java 23で世代別ZGCがデフォルト • ZGCで非世代別アルゴリズムを削除 -
491: Synchronize Virtual Threads without Pinning • 仮想スレッドでsynchronizedブロックを動かすときにプラット フォームスレッドにピン留めしていた •
これがプラットフォームスレッドの枯渇やデッドロックの原因に なっていた • ピン留めしないように変更 -
Security • 478: Key Derivation Function API (Preview) • 486:
Permanently Disable the Security Manager • 496: Quantum-Resistant Module-Lattice-Based Key Encapsulation Mechanism • 497: Quantum-Resistant Module-Lattice-Based Digital Signature Algorithm -
Quantum Resistant Algorithm • 496: Quantum-Resistant Module-Lattice-Based Key Encapsulation Mechanism
• 497: Quantum-Resistant Module-Lattice-Based Digital Signature Algorithm • will back-port to LTS