-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptionalClass.java
More file actions
51 lines (38 loc) · 1.18 KB
/
Copy pathOptionalClass.java
File metadata and controls
51 lines (38 loc) · 1.18 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
package java8_features;
import java.util.Optional;
/**
* Created by nector on 23/02/17.
*/
public class OptionalClass {
public static void main(String[] args) {
OptionalClass optionalClass = new OptionalClass();
optionalClass.optionalParam(Optional.ofNullable(15));
optionalClass.optionalParam(Optional.ofNullable(null));
Optional<String> string = optionalClass.optionalReturn(false);
if (string.isPresent()) {
System.out.println("Result not null");
} else {
System.out.println("Result null");
}
}
/**
* Passing a Optional as a parameter shouldn't be done.
* This is just a example.
*
* @param optParam
*/
private void optionalParam(Optional<Integer> optParam) {
if (optParam.isPresent()) {
System.out.println("You passed in a parameter");
} else {
System.out.println("The parameter is null");
}
}
/**
* @param b
* @return a valid string if b == true, else null
*/
private Optional<String> optionalReturn(boolean b) {
return b ? Optional.of("This is a string") : Optional.empty();
}
}