Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
JEP530 : Primitive Types in Patterns, instanceo...
Search
riko
March 24, 2026
Technology
76
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
JEP530 : Primitive Types in Patterns, instanceof, and switch (Fourth Preview)
riko
March 24, 2026
Transcript
17 slides
JEP530: Primitive Types in Patterns, instanceof, and switch (Fourth Preview)
2026/03/24 JJUGナイトセミナー 「Java 26 リリース記念イベント」
自己紹介 2
JEP530の概要 「すべてのプリミティブ型を、 パターンマッチングで扱えるようにする」 以下の3つでプリミティブ型がフルサポートされる 1. instanceof演算子 2. switch文 / switch式
3. Record Pattern 3
プリミティブ型でパターンマッチング① int i = ...; if(i >= Byte.MIN_VALUE && <=
Byte.MAX_VALUE){ byte b = (byte)i; ... 4 int i = ...; if(i instanceof byte b){ ... こう書けるように • instanceofでプリミティブ型が使える
プリミティブ型でパターンマッチング② 5 long id = ...; switch(id){ case 1L ->
{} case 2L -> {} default -> {} } • 今までのswitchで使えたもの • int・char・byte・shortとそれらのラッパー, String, enum • 新しく使えるようになるもの • long, float, double, boolean
プリミティブ型でパターンマッチング③ 6 void process(Number n){ switch (n){ case int _
-> {} case double _ -> {} default -> {} } } • switchのcaseラベルでプリミティブ型が使える
プリミティブ型でパターンマッチング④ 7 record Point(int x, int y){} Object obj =
... //←ObjectとしてPointが渡された if(obj instanceof Point(byte x, byte y)){ ... // Pointの中身がbyteに収まる場合のみ処理 • レコードパターンと統合
リリースの歴史 JEP455 JDK23 Preview ↓ JEP488 JDK24 Second Preview ↓
JEP507 JDK25 Third Preview ↓ JEP530 JDK26 Fourth Preview 8
今回の変更点 2つの変更点が提案 1. 無条件の正確性(unconditional exactness)の定義の強化 2. switch構文における、より厳密な支配(dominance)チェックの 適用 9
Dominance(支配)の原則 • 到達不可能なcaseを防ぐため、広範なパターンが先に来たら コンパイラはエラーを出さなければならない Object o = "test"; switch (o){
case Object _ -> {} case String _ -> {} default -> {} } 10
パターンの「支配」判定の複雑化 • 参照型の場合はシンプル: • ObjectはStringを支配する(親クラスは子クラスを完全に覆う) という明確なルールがあった • プリミティブの導入による混乱 • 「intはbyteを支配するのか?」「Integerはintを支配するのか?」
int x = …; switch (x){ case byte _ -> {} case int _ -> {} default -> {} }; xの値によって当てはまるcaseが変わる 11
Unconditional Exactness(無条件の正確性)とは? 常に安全な変換かどうかの基準 • ある型Aから型Bへの変換が、実行時の具体的な値に依存せず、 常に情報損失なく(Exactlyに)行えること • byte → int:Unconditionally
Exactである • どんなbyteの値も絶対にintに収まる • int → byte:Unconditionally Exactではない • 100なら収まるが、1000なら収まらないなど、値に依存する • int → float:Unconditionally Exactではない • 大きな桁のintは、floatにすると下位の精度が落ちる 12 例
JEP530での再定義 ある型Tが型Uを支配するのは、UからTへの変換が Unconditionally Exactである場合に限る • コンパイラが、数学的に矛盾なく「到達不能コード」を判定し、 適切なエラーを出すようになった 13 int j
= …; switch (j){ case float f -> {} case 16_777_216 -> {} default -> {} };
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 例
2種類のUnconditionally Exact • Value-based • 定数式限定 コンパイラが値そのものを解析して、情報欠落がないと判定されたもの 15 byte x
= …; switch (x){ case 100 -> {} case 1000 -> {} default -> {} };
互換性が失われたもの • 以前はOKだった • OKだと、開発者がBに到達可 能性があると誤解するかも • 今回からエラーになった 16 interface
A {} interface B {} A a = …; switch (a){ case A -> {} case B -> {} };
まとめ • 統一感の向上 • プリミティブとオブジェクトの文法的な壁がなくなり、 一貫したコードが書ける • 安全性の向上 • オーバーフローや精度落ちをコンパイル時に安全にチェック
17
More Decks by riko
See All by riko
JEP 527:Post-Quantum Hybrid Key Exchange for TLS 1.3
riko111
0
48
Other Decks in Technology
See All in Technology
ラクスル会社紹介資料|エンジニア向け / RAKSUL Introduction for Engineers
raksulrecruiting
0
130
2026-11-11 Tamachi.sre#7 PaCにインフラリソースの命を救われました。 / PaC saves the infra resource life
masasuzu
1
200
Quick Automate | 目指せ Quick マスター 12
inariku
PRO
1
120
狙われているAPIの脆弱性!今こそ手をつけるべき根本的な対策とは?
nagix
0
140
おうち環境でも「なるべく止めたくない!」 ― おうち環境の機器冗長化の理想と現実
yuutosi
1
520
Where Should Platform Complexity Live? Lessons from a Cross-Team PR Preview Platform
taiki45
0
120
Claude Codeで新規事業を伸ばせるか? Zennの試行錯誤の記録
wadayusuke
2
230
Context parameters で作る!はじめての型クラス
kworkdev
PRO
1
130
小さなエージェントから始めてみよう!実践コンテキストエンジニアリング
iwamot
PRO
0
130
お風呂場をサーバルームにしよう
yusuke427
0
290
SSFの現在地 ― CAEP/RISCからAI Identityへ
oidfj
0
460
契約書SaaSにAIエージェントを組み込む
sansantech
PRO
0
170
Featured
See All Featured
Building AI with AI
inesmontani
PRO
1
1.4k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
3k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
18k
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
830
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
640
SEO for Brand Visibility & Recognition
aleyda
0
4.9k
Writing Fast Ruby
sferik
630
63k
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
1.1k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.9k
The Curious Case for Waylosing
cassininazir
1
610