optional
-
Optional, 그렇게 쓸꺼야🤔?스터디 노트 2023. 12. 5. 15:15
질의를 받았습니다. 여기에 왜 Optional을 쓰셨어요? 음? null을 방지하기 위해서...? 답변하기가 애매했습니다. Optional은 왜 써야 할까요? 삼항연산자로 가볍게 처리하면 되는데 굳이 Optional을 써야만 하는걸까요? 📌 Optional이 뭡니까? Java의 Optional은 null 값을 감싸서(Wrapping) 보다 안전하게 다루기 위한 목적으로 도입된 클래스입니다. Optional의 기본 개념은 다음과 같습니다. Optional은 null 값을 감싸는 객체입니다. Optional은 값이 있는지 여부를 확인할 수 있습니다. Optional에 값이 있는 경우, 값을 가져올 수 있습니다. Optional에 값이 없는 경우, 지정된 값을 반환하거나, 지정된 함수를 호출하여 반환된 값을 ..
-
Java Optional의 메소드 사용 설명서(Optional 잘 활용하기)스터디 노트 2023. 10. 16. 15:50
원본 강의 영상 : https://youtu.be/RsUTolCVm_E?si=JRr_Xu73RmoaybFP 📌 Optional이란? Java의 Optional 함수는 JDK 8에서 추가가 되었습니다. Optional은 쉽게 말해 있거나 없는 값을 표현할 수 있는 클래스입니다. 즉, null을 대체할 수 있는 타입입니다. null(💩)은 언제나 위기를 야기하기에 잘 다루어야 합니다 📌 Optional 만들기 Optional을 생성할 수 있는 방법은 of(), ofNullable(), empty()를 통해 생성할 수 있습니다. // of() : null이 아닌 값으로 생성됨 Optional opt = Optional.of("value"); // 만약 of()에 null을 넘기게 되면 NullPointerEx..