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

JavaにおけるNull非許容性

 JavaにおけるNull非許容性

2025.02.28 Server-Side Kotlin Meetup 発表資料

Yuichi.Sakuraba

February 27, 2025
Tweet

More Decks by Yuichi.Sakuraba

Other Decks in Technology

Transcript

  1. OpenJDK における新機能導入プロセス JEP: JDK Enhance Proposal Draft JEP 1st Preview

    JEP n th Preview JEP Standard JEP 最低 2 回 差し戻しあり 本日のトピックは この段階 今後、 変更される可能性大
  2. これまでの null に対する Java の取り組み null 参照型における 参照先がない状態 処理結果がない エラーなどを表す

    本来の意味 副次的な意味 型アノテーション @NonNull List<@NonNull String> texts = ... Optional Optional<String> o = Optional.ofNullable(...); @NonNull が標準になっていないなど、 取り組みが成功しているとは言いがたい ...
  3. Java の大きな流れ 重厚長大 長寿命 可変 オブジェクト 軽量 短寿命 不変 オブジェクト

    ラムダ式 (Java 8) 処理とデータの分離 Record (Java 16) Sealed (Java 17) 代数的データ型 パターンマッチング (Java 16 ~ ) 型による処理の分岐 Value Class (Java ??) 値オブジェクト
  4. Value Class 例) record Point(int x, int y) {} Point[]

    Point x y Point x y Point x y Heap Point[] x y x y x y Heap value record Point(int x, int y) {} Value Class 化 ヒープ平坦化 参照をたどる必要なし オブジェクトヘッダーなし キャッシュミス低減
  5. Null 非許容型 / Null 許容型 JEP Draft: Null-Restricted Value Class

    Types JEP Draft: Null-Restricted and Nullable Types https://openjdk.org/jeps/8316779 https://openjdk.org/jeps/8303099 String! Null 非許容型 (Null-Restricted Type) String? Null 許容型 (Nullable Type) ワイドニング変換 オブジェクト初期化順序の変更 配列初期化構文 ジェネリクス型パラメータへの適用 et al.
  6. オブジェクト初期化順序の変更 class Foo { ... } class Bar extends Foo

    { Baz baz; Bar(Baz baz) { super(); this.baz = baz; } } スーパークラスのコンストラクタは 常にコンストラクタの先頭でコール このためフィールドが未初期化状態にある class Foo { ... } class Bar extends Foo { Baz baz; Bar(Baz baz) { this.baz = baz; super(); } } スーパークラスのコンストラクタを フィールド初期化後にコール可能
  7. Conclusion 軽量不変オブジェクトへの流れ JVM 効率化としての Value Class と Null 非許容 Value

    Class でなくても Null 非許容は使用可能 決まっていないことが多いが、 期待して待ちましょう