JAVA에서 String을 Null Safe하게 비교하기 Nullable한 두 개의 String이 equal한지를 null safe하게 비교하는 방법을 알아보자. String#equals 두 String이 null이 아니면 String 클래스의 인스턴스 메소드 equals를 사용하면 된다. @Test void test0() { String s1 = "hello"; String s2 = "hello"; assertThat(s1.equals(s2)).isTrue(); s2 = "world"; assertThat(s1.equals(s2)).isFalse(); } @Test void test1() { String s1 = null; String s2 = null; assertThatThrownBy(() -> s1.equals(s2)).isInstanceOf(NullP.. 2021. 8. 14. 이전 1 다음