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
The **Optional<T>** type was introduced in Java 8 as a way to indicate that a method _may_ return a value.
4
+
5
+
Before Java 8, developers had to implement null checks:
6
+
7
+
```java
8
+
publicEmployee getEmployee(String name) {
9
+
// Assume that getEmployeeByName retrieves an Employee from a database
10
+
Employee employee = getEmployeeByName(name);
11
+
if (employee !=null) {
12
+
return employee;
13
+
} else {
14
+
thrownewIllegalArgumentException("Employee not found");
15
+
}
16
+
}
17
+
```
18
+
19
+
It is important to understand that the Optional API does not eliminate the null checking,
20
+
but it defers it until the end of a series of methods, as long as all those methods return an optional object.
21
+
22
+
TBD: Rephrase this last paragraph.
23
+
24
+
The Optional type is mainly used as returned type. Using it as a parameter type or field type is less common and
25
+
not recommended, as explained by one well-known Java language architect in [this SO answer](https://stackoverflow.com/questions/26327957/should-java-8-getters-return-optional-type).
Copy file name to clipboardexpand all lines: exercises/concept/tim-from-marketing-2/.docs/introduction.md
+13-18
Original file line number
Diff line number
Diff line change
@@ -2,24 +2,24 @@
2
2
3
3
## Optional
4
4
5
-
## Introduction
5
+
The **Optional<T>** type was introduced in Java 8 as a way to indicate that a method _may_ return a value.
6
6
7
-
The **Optional<T>** type was introduced in Java 8 as a way to indicate that a method will return an object of type T or an empty value. It is present in type signatures of many core Java methods.
8
-
Before Java 8, developers had to implement null checks:
7
+
In other words, there is a chance the method returns "no value" at all.
8
+
9
+
## Creating an Optional<T> object
10
+
11
+
Given an object of type Employee, an Optional<Employee> object is created as follows:
9
12
10
13
```java
11
-
publicEmployee getEmployee(String name) {
12
-
// Assume that getEmployeeByName retrieves an Employee from a database
13
-
Employee employee = getEmployeeByName(name);
14
-
if (employee !=null) {
15
-
return employee;
16
-
} else {
17
-
thrownewIllegalArgumentException("Employee not found");
@@ -62,8 +62,3 @@ public Optional<Integer> getEmployeeAge(String name) {
62
62
.orElse("No employee found");
63
63
}
64
64
```
65
-
66
-
It is important to understand that the Optional API does not eliminate the null checking,
67
-
but it defers it until the end of a series of methods, as long as all those methods return an optional object.
68
-
The Optional type is mainly used as returned type. Using it as a parameter type or field type is less common and
69
-
not recommended, as explained by one well-known Java language architect in [this SO answer](https://stackoverflow.com/questions/26327957/should-java-8-getters-return-optional-type)
Copy file name to clipboardexpand all lines: exercises/concept/tim-from-marketing-2/.meta/design.md
+3-3
Original file line number
Diff line number
Diff line change
@@ -3,9 +3,8 @@
3
3
## Goal
4
4
5
5
The goal of this exercise is to teach the student how to use the Optional API.
6
-
We will use the most common methods: `ifPresent`, `orElse`, `ifPresentOrElse`, `orElseThrow`.
6
+
We will use the most common methods: `ifPresent`, `orElse`, `ifPresentOrElse`, `orElseThrow`.
7
7
The `isPresent` and `get` methods are not presented, since they do not provide any value over an ordinary null check.
8
-
Some methods of the Stream API are needed. This is a bit problematic, since they have not been explained in the current Java track.
9
8
10
9
## Learning objectives
11
10
@@ -35,8 +34,9 @@ This Concept Exercise's prerequisites Concepts are:
35
34
## Analyzer
36
35
37
36
This exercise could benefit from the following rules in the [analyzer]:
37
+
38
38
-`essential`: If the solution uses `null` in any method, encourage the student to use `Optional<T>` instead.
39
-
-`actionable`: If the solution uses the `get` or `isPresent` methods of the Optional<T> API, encourage the student to use `orElse`, `orElseThrow` or `ifPresentOrElse` instead.
39
+
-`actionable`: If the solution uses the `get` or `isPresent` methods of the Optional<T> API, encourage the student to use `orElse`, `orElseThrow` or `ifPresentOrElse` instead.
40
40
-`informative`: TODO.
41
41
42
42
If the solution does not receive any of the above feedback, it must be exemplar.
0 commit comments