Created
March 28, 2014 14:19
-
-
Save bugabinga/9833966 to your computer and use it in GitHub Desktop.
Java Optional type example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MaybeBaby { | |
public final Optional<String> data; | |
public MaybeBaby(String data) { | |
this.data = Optional.of(data) ; | |
} | |
public MaybeBaby() { | |
data = Optional.empty() ; | |
} | |
public static main(String[] args) { | |
MaybeBaby no = new MaybeBaby() ; | |
MaybeBaby yes = new MaybeBaby("yes") ; | |
no.ifPresent(data -> System.out.println("will never run")) ; | |
// will print yes | |
yes.ifPresent(System.out::println) ; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment