2023-java-study / book-study

북 스터디 기록 레포지토리
0 stars 0 forks source link

[Item 10] null도 정상 값으로 취급하는 참조 타입 필드 - Objects.equals(obj, obj) 사용하기 #31

Closed gmelon closed 1 year ago

gmelon commented 1 year ago

Objects.equals(Object object1, Object object2)에 null이 들어가면 false가 반환되는지?

gmelon commented 1 year ago
@Test
void objects_equals() {
    Objects.equals(null, new MyClass()); // always false
}

static class MyClass {
}

-> 동작 중 null이 들어갈 수 있게 설계된 참조 객체 필드는 Objects.equals() 로 비교하기