Share
Embed
Java 22 Overview
Material for Java 22 release event in Fukuoka at 2024-4-16
https://javaq.connpass.com/event/315157/
Naoki Kishida
April 16, 2024
Transcript
27 slides
-
2024/04/18 2 自己紹介 • きしだ なおき • LINEヤフー • twitter:
@kis • 「プロになるJava」という Java入門書を書いてます -
Java 22 • 3/19 released • non LTS • Features
• 12 JEPs(JDK Enhancement Proposals) • Major changes are sumarized as JEP • Stream Gatherer(Preview) • Foreign Function & Memory API is now standard • simple "public static void main" (2nd Preview) -
Java Playground • Try Java in browser • https://dev.java/playground/ •
code snipet for new features as sample • JDK works on server side • not in browser -
2024/04/18 5 non-LTS • Java 22 is non-LTS(Long Term Support)
• It will not be a candidate to use in our product • the support will finish after Java 23 is released • Next LTS will be Java 25(Sept. 2025) -
Java 22 Distribution • Oracle OpenJDK • Oracle JDK •
Adoptium Temurin • Amazon Corretto 22 • Liberica JDK • SapMachine • Azul Zul no release for Microsoft build -
2024/04/18 7 Outline • 12 JEPs (1/2) • Language •
JEP 447: Statements before super(...) (Preview) • JEP 456: Unnamed Variables & Patterns • JEP 459: String Templates (Second Preview) • 463: Implicitly Declared Classes and Instance Main Methods (Second Preview) • Tool • 458: Launch Multi-File Source-Code Programs -
2024/04/18 8 Outline • 12 JEPs(2/2) • API • JEP
454: Foreign Function & Memory API • JEP 457: Class-File API (Preview) • JEP 460: Vector API (Seventh Incubator) • JEP 461: Stream Gatherers (Preview) • JEP 462: Structured Concurrency (Second Preview) • JEP 464: Scoped Values (Second Preview) • JVM • JEP 423: Region Pinning for G1 -
incompatibility • direct execution for source file require strict path
coresponding to the package package hello; public class Hello { public static void main(String[] args) { System.out.println("Hello"); } } C:\Users\naoki\Desktop>java hello.java エラー: ソース・ファイルへのパスの終わりがパッケージ名helloと一致し ません: hello.java C:\Users\naoki\Desktop>mkdir hello C:\Users\naoki\Desktop>move hello.java hello 1 個のファイルを移動しました。 C:\Users\naoki\Desktop>java hello/hello.java Hello -
Language change • Finalized • Unnamed Variables & Patterns •
Preview • Statements before super(...) • String Tempate • Implicitly Declared Classes and Instance Main Methods -
Unnamed Variables & Patterns • unused variable can be defined
as ‘_’ • variable • local variable • try-with-resource • for • enhanced for • catch • lambda parameter Variable catch (InterruptedException _) { // do nothing } IntStream.range(0, 10) .forEach(_ -> System.out.println(“hello”)) Pattern case NegExpr(_) -> “negative” -
Statements before super(preview) • enable to write statements without `this`
before `super` invocation 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() { // statement before super var param = List.of("abc", "def"); super(param, param); } } when we want to pass same list for each args in Java 22 -
String tempates(2nd Preview) • String interpolation • STR.”Hello \{name}” •
will be 3rd preview in JDK 23 • JDK 22 has 3 templates. STR, FMT, RAW jshell> STR."Today is \{new Date()} now" $1 ==> "Today is Wed Sep 20 05:34:20 JST 2023 now" jshell> import static java.util.FormatProcessor.FMT jshell> FMT."a=%05d\{a} d=%.3f\{d}" $9 ==> "a=00123 d=3.140" jshell> StringTemplate.RAW."a=\{a} d=\{d}" $11 ==> StringTemplate{ fragments = [ "a=", " d=", "" ], values = [123, 3.14] } -
Implicitly Declared Classes and instance main method(2nd preview) • 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! void main() { System.out.println(“Hello world!”); } -
From for large program to for small program • Java
had aimed to write large systems reliably • Object Oriented • Redundant syntax • Core for studying or checking is boring • require compilation • require a lot of typing just for `main` • On teaching, need to turn a blind eye for `public static` • cannot be explained unless learning progresses • Everyone uses Python -
More simple • JEP draft 8323335 • println • print/println
will be declared as static method in java.io.SimpleIO • auto import • import all classes automatically in java.base module import java.util.List; public class Sample { public static void main(String[] args) { var data = List.of("abc", "defg", "nnn"); for (var s : data) { System.out.println(data); } } } void main() { var data = List.of("abc", "defg", "nnn"); for (var s : data) { println(data); } } -
More simple • module import • JEP draft 8315129 •
import all classes in a module import java.util.Collections; import java.util.List; import java.util.stream.Collectors; public class ModuleSample { public static void main(String[] args) { var data = List.of("abc", "defg", "nnn"); var doubled = data.stream() .map(s -> s.repeat(2)) .collect(Collectors.toList()); Collections.shuffle(doubled); System.out.println(doubled); } } import module java.base; import static java.io.SimpleIO.*; public class ModuleSample { void main() { var data = List.of("abc", "defg", "nnn"); var doubled = data.stream() .map(s -> s.repeat(2)) .collect(Collectors.toList()); Collections.shuffle(doubled); println(doubled); } } -
Tool: Launch Multi-File Source-Code Programs • Enable to directly execute
with multi-file source code program using `java` command -
API • 454: Foreign Function & Memory API • 457:
Class-File API (Preview) • 460: Vector API (Seventh Incubator) • 461: Stream Gatherers (Preview) • 462: Structured Concurrency (Second Preview) • 464: Scoped Values (Second Preview) -
Stream Gatherers(Preview) • WIth Stream, it was not possible to
write processed that reference other data • like sliding window • Stream Gatherer makes it possible • Gatherers like Collectors for `collect` 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 -
Foreign Function & Memory API • nativfe access features now
standard • native memory access • enable to allocate off-heap memory that is not managed by GC • Foreign function call • native libraries can be called -
Vector API(7th 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 -
Class-File API(Preview) • 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 -
Scoped Values(Preview) • Sharing a value among methods on thread
safe • ThreadLocal is too heavy to the purpose -
Structured Concurrency(Preview) • When building single unit of task by
different thread, synchronizing the subtask is required. But using `wait` and `join` will make “go-to hell” like complexty. • Structured concurrency preserves the natural relationship between tasks and subtasks. try (var scope = new StructuredTaskScope.ShutdownOnFailure()) { Future<String> user = scope.fork(() -> findUser()); Future<Integer> order = scope.fork(() -> fetchOrder()); scope.join(); // Join both forks scope.throwIfFailed(); // .// and propagate errors // Here, both forks have succeeded, so compose their results return new Response(user.resultNow(), order.resultNow()); } -
What's next? • JEP 468: Derived Record Creation(Preview) • JEP
467: Markdown Documentation Comments record Point(int x, int y, int z) { } Point oldLoc = new Point(1, 2, 3); Point nextLoc = oldLoc with { x *= 2; y *= 2; z *= 2; };