Upgrade to Pro — share decks privately, control downloads, hide ads and more …

JEP530 : Primitive Types in Patterns, instanceo...

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →
Avatar for riko riko
March 24, 2026

JEP530 : Primitive Types in Patterns, instanceof, and switch (Fourth Preview)

Avatar for riko

riko

March 24, 2026

More Decks by riko

Other Decks in Technology

Transcript

  1. JEP530: Primitive Types in Patterns, instanceof, and switch (Fourth Preview)

    2026/03/24 JJUGナイトセミナー 「Java 26 リリース記念イベント」
  2. プリミティブ型でパターンマッチング① int i = ...; if(i >= Byte.MIN_VALUE && <=

    Byte.MAX_VALUE){ byte b = (byte)i; ... 4 int i = ...; if(i instanceof byte b){ ... こう書けるように • instanceofでプリミティブ型が使える
  3. プリミティブ型でパターンマッチング② 5 long id = ...; switch(id){ case 1L ->

    {} case 2L -> {} default -> {} } • 今までのswitchで使えたもの • int・char・byte・shortとそれらのラッパー, String, enum • 新しく使えるようになるもの • long, float, double, boolean
  4. プリミティブ型でパターンマッチング③ 6 void process(Number n){ switch (n){ case int _

    -> {} case double _ -> {} default -> {} } } • switchのcaseラベルでプリミティブ型が使える
  5. プリミティブ型でパターンマッチング④ 7 record Point(int x, int y){} Object obj =

    ... //←ObjectとしてPointが渡された if(obj instanceof Point(byte x, byte y)){ ... // Pointの中身がbyteに収まる場合のみ処理 • レコードパターンと統合
  6. リリースの歴史 JEP455 JDK23 Preview ↓ JEP488 JDK24 Second Preview ↓

    JEP507 JDK25 Third Preview ↓ JEP530 JDK26 Fourth Preview 8
  7. Unconditional Exactness(無条件の正確性)とは? 常に安全な変換かどうかの基準 • ある型Aから型Bへの変換が、実行時の具体的な値に依存せず、 常に情報損失なく(Exactlyに)行えること • byte → int:Unconditionally

    Exactである • どんなbyteの値も絶対にintに収まる • int → byte:Unconditionally Exactではない • 100なら収まるが、1000なら収まらないなど、値に依存する • int → float:Unconditionally Exactではない • 大きな桁のintは、floatにすると下位の精度が落ちる 12 例
  8. 2種類のUnconditionally Exact • Type-based • 入力値にかかわらず、型の性質のみで安全性が保証される変換 14 int x =

    … switch (x){ case int _ -> {} case byte _ -> {} default -> {} }; byte→ int, int→long, float→double int→Integer, String→Object 例