728x90
equals() 를 사용할 때
비교하는 예시가 null 일 경우 에러가 난다.
NullPoitException
public class Equals {
public static void main(String[] args) {
String a = null;
String b = "b";
if(a != null & aa.equals(b)) {
System.out.println("a equal to b");
}
}
}
굳이 널 여부까지 체크해줘야된다는 말임
public class Equals {
public static void main(String[] args) {
String a = null;
String b = null;
String c = "apple";
String d = "apple";
System.out.println(Objects.equals(a, b)); // true
System.out.println(Objects.equals(c, d)); // true
System.out.println(Objects.equals(a, c)); // false
}
}
NULL값도 포함해서 비교가 가능하다
728x90
'Web > JAVA' 카테고리의 다른 글
[Generics] 제네릭 알아보기 (0) | 2023.11.29 |
---|---|
[단일요소배열] Collections.singletonList 와 Arrays.asList (1) | 2023.11.25 |
SpringTokenizer (0) | 2023.10.19 |
데이터 구조 (1) | 2023.10.19 |
[Java Stream] skip() vs limit() (0) | 2023.04.23 |