Skip to content

Optional 소개

Jeonghyun Kang edited this page May 21, 2022 · 3 revisions

이전의 방식

그냥 Null을 반환하고 클라이언트 코드에서 체크

Progress progress = spring_boot.getProgress();
if (progress != null) {
    System.out.println(progress.getStudyDuration());
}

Null일 경우 예외처리

public Progress getProgress() {
    if (this.progress == null) {
        throw new IllegalStateException();
    }
    return progress;
}

### Java 8부터
```java
  • nullable: Optional에 들어가는 값이 Null일 수도 있을 때 사용.
  • Optional은 왠만해서는 return 타입으로만 쓴다.
    • 그 이유는

Clone this wiki locally