yohhoyの日記

技術的メモをしていきたい日記

Underscore As A Keyword

Java 9では1文字アンダースコア(_)はキーワード(keyword)に変更され、変数名などに利用するとコンパイルエラーになる。Java 8以前は非推奨の識別子(identifier)であり、コンパイル時の警告メッセージにとどまっていた。

class UAAK {
  public static void main(String[] args) {
    int _ = 42;
  }
}

Java 8コンパイル警告メッセージ:

warning: '_' used as an identifier
(use of '_' as an identifier might not be supported in releases after Java SE 8)

Java 9コンパイルエラー:

error: as of release 9, '_' is a keyword, and may not be used as an identifier

関連URL