You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 05-optionals.md
+42-30Lines changed: 42 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,7 @@
1
-
## Optionals
1
+
Optionals
2
+
----
2
3
3
-
Every Java developer whether beginner, novice, or seasoned has in his/her lifetime experienced `NullPointerException`. This is a true fact that no Java developer can deny. We all have wasted or spent many hours trying to fix bugs caused by `NullPointerException`. According to `NullPointerException` JavaDoc, ***NullPointerException is thrown when an application attempts to use null in a case where an object is required.***. This means if we invoke a method or try to access a property on ***null*** reference then our code will explode and `NullPointerException` is thrown.
4
+
Every Java developer whether beginner, novice, or seasoned has in his/her lifetime experienced `NullPointerException`. This is a true fact that no Java developer can deny. We all have wasted or spent many hours trying to fix bugs caused by `NullPointerException`. According to `NullPointerException` JavaDoc, ***NullPointerException is thrown when an application attempts to use null in a case where an object is required.***. This means if we invoke a method or try to access a property on ***null*** reference then our code will explode and `NullPointerException` is thrown. In this chapter, you will learn how to write null free code using Java 8 `Optional`.
4
5
5
6
> On a lighter note, if you look at the JavaDoc of NullPointerException you will find that author of this exception is ***unascribed***. If the author is unknown unascribed is used(nobody wants to take ownership of NullPointerException ;))
6
7
@@ -14,6 +15,8 @@ Most of the programming languages like C, C++, C#, Java, Scala, etc. has nullabl
14
15
15
16
Let's look at the example Task management domain classes shown below. Our domain model is very simple with only two classes -- Task and User. A task can be assigned to a user.
16
17
18
+
> Code for this section is inside [ch05 package](https://github.com/shekhargulati/java8-the-missing-tutorial/tree/master/code/src/main/java/com/shekhargulati/java8_tutorial/ch05).
19
+
17
20
```java
18
21
publicclassTask {
19
22
privatefinalString id;
@@ -53,7 +56,7 @@ public class Task {
53
56
publicclassUser {
54
57
55
58
privatefinalString username;
56
-
privateStringfullname;
59
+
privateStringaddress;
57
60
58
61
publicUser(Stringusername) {
59
62
this.username = username;
@@ -63,12 +66,12 @@ public class User {
63
66
return username;
64
67
}
65
68
66
-
publicStringgetFullname() {
67
-
returnfullname;
69
+
publicStringgetAddress() {
70
+
returnaddress;
68
71
}
69
72
70
-
publicvoidsetFullname(Stringfullname) {
71
-
this.fullname=fullname;
73
+
publicvoidsetAddress(Stringaddress) {
74
+
this.address=address;
72
75
}
73
76
}
74
77
```
@@ -139,53 +142,62 @@ Let's update our domain model to reflect optional values.
Use of `Optional` data type in the data model makes it explicit that `Task` refers to an ***Optional<User>*** and ***User*** has an **Optional<String>** username. Now whoever tries to work with `assignedTo` User would know that it might not be present and they can handle it in a declarative way. We will talk about `Optional.empty` and `Optional.of` methods in the next section.
@@ -196,7 +208,7 @@ In the domain model shown above, we used couple of creational methods of the Opt
196
208
197
209
***Optional.empty**: This is used to create an Optional when value is not present like we did above `this.assignedTo = Optional.empty();` in the constructor.
198
210
199
-
***Optional.of(T value)**: This is used to create an Optional from a non-null value. It throws `NullPointerException` when value is null. We used it in the code shown above `this.fullname = Optional.of(fullname);`.
211
+
***Optional.of(T value)**: This is used to create an Optional from a non-null value. It throws `NullPointerException` when value is null. We used it in the code shown above `this.address = Optional.of(address);`.
200
212
201
213
***Optional.ofNullable(T value)**: This static factory method works for both null and non-null values. For null values it will create an empty Optional and for non-null value it will create Optional using the value.
202
214
@@ -220,7 +232,7 @@ public class TaskRepository {
220
232
221
233
## Using Optional values
222
234
223
-
Optional can be thought as a Stream with one element. It has methods similar to Stream API like map, filter, flatmap that we can use to work with values contained in the `Optional`.
235
+
Optional can be thought as a Stream with one element. It has methods similar to Stream API like map, filter, flatMap that we can use to work with values contained in the `Optional`.
0 commit comments