-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptionalStartMain1.java
More file actions
39 lines (29 loc) · 988 Bytes
/
Copy pathOptionalStartMain1.java
File metadata and controls
39 lines (29 loc) · 988 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
39
package optional;
import java.util.HashMap;
import java.util.Map;
public class OptionalStartMain1 {
private static final Map<Long, String> map = new HashMap<>();
static {
map.put(1L, "Kim");
map.put(2L, "Seo");
}
public static void main(String[] args) {
findAndPrint(1L); // 값이 있는 경우
findAndPrint(2L); // 값이 없는 경우
}
// 이름이 있으면 이름을 대문자로 출력, 없으면 "UNKNOWN"을 출려갛라
static void findAndPrint(Long id){
String name = findNameById(id);
// 1. NullPointerException 유발
// System.out.println("name = " + name.toUpperCase());
// 2. if 문을 활용한 null 체크 필요
if(name != null){
System.out.println(id + ": " + name.toUpperCase());
} else {
System.out.println(id + ": " + "UNKNOWN");
}
}
static String findNameById(Long id){
return map.get(id);
}
}