Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
493 views
in Technique[技术] by (71.8m points)

nullpointerexception - Null check in Java 8 Elvis operator?

Question: Is there an implementation of the Elvis operator scheduled for any future Java release? Or is there any Library that brings it to Java?

I have read that

it was proposed for Java SE 7 but didn't make it into that release

http://www.oracle.com/technetwork/articles/java/java8-optional-2175753.html

I know Java 8 allows this

String name = computer.flatMap(Computer::getSoundcard)
                          .flatMap(Soundcard::getUSB)
                          .map(USB::getVersion)
                          .orElse("UNKNOWN");

but I it's s bit too much for my taste. SO if anyone could point me out any project / library that would bring the Groovy like/C# like syntax to Java for Null Checks, would be greatly appreciated.

Edit: By Elvis operator I mean this:

String version = computer?.getSoundcard()?.getUSB()?.getVersion();

or similar

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

No. There are no current or future plans to reconsider the null-safe operators in Java.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...