forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptionalsEx.java
More file actions
39 lines (30 loc) · 946 Bytes
/
OptionalsEx.java
File metadata and controls
39 lines (30 loc) · 946 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.zetcode;
import java.util.Optional;
public class OptionalsEx {
public static void main(String[] args) {
if (getNullMessage().isPresent()) {
System.out.println(getNullMessage().get());
} else {
System.out.println("n/a");
}
if (getEmptyMessage().isPresent()) {
System.out.println(getEmptyMessage().get());
} else {
System.out.println("n/a");
}
if (getCustomMessage().isPresent()) {
System.out.println(getCustomMessage().get());
} else {
System.out.println("n/a");
}
}
private static Optional<String> getNullMessage() {
return Optional.ofNullable(null);
}
private static Optional<String> getEmptyMessage() {
return Optional.empty();
}
private static Optional<String> getCustomMessage() {
return Optional.of("Hello there!");
}
}