●Null-Dereference Expression
Nullチェックの方法が簡潔に記述できるようになります。
http://docs.google.com/View?docid=dfn5297z_19pnsjkfc6
http://www.slideshare.net/Stephan.Schmidt/better-strategies-for-null-handling-in-java
句、節の最後に"?"で記述します。Groovyの表記を真似したそうです。
■使用例
//このように書くと String str = getStringValue() ?: ""; //このように解釈されます。 String str = null; if( getStringValue() != null ) { str = getStringValue(); } else { str = ""; }
//このように書くと T x = company?.branch()?.employee()?.id(); //このように解釈されます。 T x = null; if( company != null ) { Branch branch = company.branch(); if( branch != null ) { Employee employee = branch.employee(); if( employee != null ) { id = employee.id(); } } }
※当初はこのような提案もされていたみたいです。
http://docs.google.com/View?docid=dfn5297z_2kjj2fk
JSR308の@NonNullがありますから必要なくなったのかもしれません。
//このように書くとコンパイルエラー #Company c = null; //#つけたものはnullを代入できない。