티스토리 뷰
isEmpty()에 대해 알아보겠습니다.
isEmpty()는 String 변수의 값의 존재 여부를 판단하는 메서드입니다.
String a = "";
String b = null;
System.out.println(a.isEmpty()); // 결과: true
System.out.println(b.isEmpty()); // 결과: NullPointerException
String.java안에 method들 중 아래와 같은 method로 선언되어 있는 걸 확인할 수 있습니다.
public boolean isEmpty(){
return value.length == 0;
}
isEmpty()를 int 변수에 사용하게 되면 java: int cannot be dereferenced와 같은 에러 메시지를 확인할 수 있습니다.
list를 사용할 때도 isEmpty()를 사용할 수 있습니다.
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
if (list.isEmpty()) {
System.out.println("true");
} else {
System.out.println("false");
}
List<String> list = new ArrayList();
if (list.isEmpty()) {
System.out.println("true");
} else {
System.out.println("false");
}
결과 : true
이와 같이 isEmpty()를 사용해 데이터의 존재 여부를 판단하여 코드를 작성할 수 있습니다.
끝.
728x90
'Java' 카테고리의 다른 글
[Java] distinct() (0) | 2022.12.15 |
---|---|
[Java] Iterator() (0) | 2022.11.02 |
[Java] StringUtils.equals() (0) | 2022.09.15 |
[Java] contains() (0) | 2022.06.30 |
[Spring] CollectionUtils.isEmpty() (0) | 2022.06.20 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Built-in Functions
- index
- Python
- function
- bool
- find
- combinations
- operators
- isdigit
- isalpha
- If
- zip
- Lower
- Method
- Lambda
- Upper
- permutations
- counter
- for
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
글 보관함