Created
June 23, 2020 09:35
-
-
Save JHarry444/c682803a393e93f28683d609949a7835 to your computer and use it in GitHub Desktop.
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
package main; | |
import java.util.Optional; | |
public class Optionals { | |
public static void main(String[] args) { | |
// String isItNull = isItNull(); | |
// if (isItNull != null) { | |
// System.out.println(isItNull.length()); | |
// } else { | |
// isisItNull = "piers is a weapon"; | |
// } | |
// System.out.println(isItNull().length()); | |
// System.out.println(isItNull().length()); | |
// System.out.println(isItNull().length()); | |
// System.out.println(isItNull().length()); | |
// System.out.println(isItNull().length()); | |
for (int i = 0; i < 10; i++) { | |
System.out.println(isItNull().get()); | |
System.out.println(isItNull().orElse("piers is a weapon")); | |
System.out.println(isItNull().orElseThrow(() -> new RuntimeException())); | |
} | |
} | |
private static Optional<String> isItNull() { | |
String out = null; | |
if (Math.random() > 0.5) { | |
out = "optional"; | |
} | |
return Optional.ofNullable(out); | |
} | |
// private static String isItNull() { | |
// String out = null; | |
// if (Math.random() > 0.5) { | |
// out = "optional"; | |
// } | |
// return out; | |
// } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment