Slide 1

Slide 1 text

Value Class で何が変わる? Java in the Box カサレアル 櫻庭 祐一

Slide 2

Slide 2 text

プリミティブ型と参照型 Value Class Value Class による最適化 プログラミングスタイルへの影響

Slide 3

Slide 3 text

プリミティブ型と参照型

Slide 4

Slide 4 text

プリミティブ型と参照型 プリミティブ型 参照型 ビルトインのみ ユーザー定義可 Null許容性 NonNull Nullable 単相/多相 単相 多相 等価 値 参照 定義

Slide 5

Slide 5 text

等価? int x = 10; int y = 10; x == y; true プリミティブ型は値で比較 Integer s = new Integer(10); Integer t = new Integer(10); s == t; false 参照型は参照で比較 値で比較 : equals()

Slide 6

Slide 6 text

等価? Integer a = Integer.valueOf(10); Integer b = Integer.valueOf(10); a == b; true Integer c = Integer.valueOf(200); Integer d = Integer.valueOf(200); c == d; false -128から127はキャッシュ リテラル文字列もキャッシュ

Slide 7

Slide 7 text

Identity オブジェクトに固有のID オブジェクトの同一性チェックに使用 プリミティブ型にはない 値が可換なオブジェクトであれば不要?? イミュータブルであれば可換になるはず ミュータブルだからIdentiyが必要

Slide 8

Slide 8 text

Integer x = Integer.valueOf(10); Heap Integer header value 10

Slide 9

Slide 9 text

int nums[] = ...; Heap header 0 1 2 3 4 10 2 1 8 4

Slide 10

Slide 10 text

record Point(int x, int y) {} Point points[] = ...; 常に参照をたどる必要あり キャッシュミスが発生しやすい Heap Point Point Point Point Point Point

Slide 11

Slide 11 text

record Point(int x, int y) {} Point points[] = ...; Heap これを実現するのが Value Class

Slide 12

Slide 12 text

Value Class

Slide 13

Slide 13 text

Value Class イミュータブルなIdentityのないクラス フィールドはすべて暗黙的にfinal finalクラス 継承 NG インタフェースの実装 OK == は値で比較 構造体ではない

Slide 14

Slide 14 text

標準APIにおけるValue Class java.lang Integer, Long, Float, Double, Byte, Short, java.util Character, Boolean Optional, OptionalInt, OptionalLong, OptionalDouble java.time LocalDate, LocalTime, LocalDateTime, ZonedDateTime, Duration 悲しいお知らせ: Stringは非Value Class

Slide 15

Slide 15 text

Value Classの書き方 valueを付記するだけ value class Foo { ... } value record Point(int x, int y) {}

Slide 16

Slide 16 text

Value Classの制限 Identityを使用する機能は使用不可 モニタ var date = LocalDate.now(); synchronized(date) { ... } IdentityException例外発生 弱参照系 WeakReference, SoftReference, PhantomReference

Slide 17

Slide 17 text

Value Classによる最適化

Slide 18

Slide 18 text

Value Classによる最適化 平坦化 Flattening オブジェクト/配列のフィールド領域に展開 63bitまで スカラー化 Scalarization 平坦化された値をメソッドのローカル変数領域に展開 最適化するかどうかはJVMが決める

Slide 19

Slide 19 text

平坦化 value class LocalDate ... { int year; byte month; byte date; ... LocalDate[] Rect } LocalDate year tl 2026 month br 11 date 29 tl br null LocalDate LocalDate year tl 2027 month br 3 date 9 year tl 2026 month br 6 date 15 LocalDate[] 1 2026 11 29 1 2026 6 15 0 null 1 2027 3 9

Slide 20

Slide 20 text

プログラミングスタイルへの影響

Slide 21

Slide 21 text

Value Classをどこで使うか イミュータブルなデータクラス 小さいデータほど効果的 レコードクラス ここに使うべき!というほどでもない

Slide 22

Slide 22 text

プログラミングスタイルへの影響 直接的な影響はない あくまでもJVM内部の効率化 宣言的記述へのきっかけ

Slide 23

Slide 23 text

プログラミングスタイルへの影響 多数の小さなオブジェクト イミュータブル NonNull 狭いスコープ

Slide 24

Slide 24 text

まだまだ続く...... Null許容性 アノテーションではなく言語仕様として JEP draft: Null-Restricted Value Class Types JEP draft: Null-Restricted and Nullable Types ジェネリクスのプリミティブ型サポート JEP 218: Generics over Primitive Types 型クラス JVMLS 2025: Growing the Java Language

Slide 25

Slide 25 text

まとめ Identity: オブジェクトの固有のID オブジェクトを区別する Value Class: Identityのないイミュータブルなクラス 値で可換なオブジェクト VCによる最適化: 平坦化、 スカラー化 現状はまだ十分ではない Project Valhallaはまだ続く...

Slide 26

Slide 26 text

参考 JEP JEP 401: Value Objects https://openjdk.org/jeps/401 JEP draft: Null-Restricted Value Class Types https://openjdk.org/jeps/8316779 JEP draft: Null-Restricted and Nullable Types https://openjdk.org/jeps/8303099 JEP 218: Generics over Primitive Types https://openjdk.org/jeps/218 Presentation Better Tools for Immutable Data (JavaOne 2026) https://youtu.be/BdLND9D81lI?si=3e3MgtHIp2xLOrg1 Value Classes Heap Flattening (JVMLS 2026) https://youtu.be/NF4CpL̲EWFI?si=TZ3fPK0ZjzYO1HUu