-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava8OptionalExample.java
More file actions
33 lines (28 loc) · 1.18 KB
/
Copy pathJava8OptionalExample.java
File metadata and controls
33 lines (28 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
import java.util.Optional;
import static java.lang.System.out;
/**
* Created by john on 2016/1/16.
*/
public class Java8OptionalExample {
public static void main(String[] args) {
Java8OptionalExample example = new Java8OptionalExample();
Integer value1 = null;
Integer value2 = new Integer(10);
//Optional.ofNullable - allows passed parameter to be null
Optional<Integer> a = Optional.ofNullable(value1);
//Optional.of - throws NullPointerException if passed parameter is null
Optional<Integer> b = Optional.of(value2);
System.out.println(example.sum(a,b));
}
public Integer sum(Optional<Integer> a, Optional<Integer> b) {
//Optional.isPresent - checks the value is present or not
out.println("First parameter is present: "+a.isPresent());
out.println("Second parameter is present: "+b.isPresent());
//Optional.orElse - return the value if present otherwise returns
// the default value pressed
Integer value1 = a.orElse(new Integer(0));
//Optional.get - get the value, value should be present
Integer value2 = b.get();
return value1 + value2;
}
}