
개선된 switch문을 사용하라는 경고이다.
AS-IS
switch (unitCode) {
case "SS": // 초
return count;
case "MI": // 분
return count * 60;
case "HH": // 시간
return count * 60 * 60;
case "DD": // 일
return count * 24 * 60 * 60;
case "WK": // 주
return count * 7 * 24 * 60 * 60;
case "MM": // 월 (30일 기준)
return count * 30 * 24 * 60 * 60;
case "YY": // 년 (365일 기준)
return count * 365 * 24 * 60 * 60;
default:
return 3600;
}
TO-BE
Switch Expression (Java 14+ 표준) 방식
화살표로 표시할 수 있다.
return switch (unitCode) {
case "SS" -> // 초
count;
case "MI" -> // 분
count * 60;
case "HH" -> // 시간
count * 60 * 60;
case "DD" -> // 일
count * 24 * 60 * 60;
case "WK" -> // 주
count * 7 * 24 * 60 * 60;
case "MM" -> // 월 (30일 기준)
count * 30 * 24 * 60 * 60;
case "YY" -> // 년 (365일 기준)
count * 365 * 24 * 60 * 60;
default -> 3600;
};
참고
https://www.jetbrains.com.cn/en-us/help/inspectopedia/EnhancedSwitchMigration.html
Statement can be replaced with enhanced 'switch' | Inspectopedia
www.jetbrains.com.cn
'Development > Java' 카테고리의 다른 글
| [Java] Refactor: Non-constant string concatenation as argument to logging call (0) | 2026.02.09 |
|---|---|
| [Java] Getter/Setter 지양과 trim 책임 분리 (feat.model 리팩토링) (3) | 2026.02.06 |
| [Java] 자바 기본 정리하기 (0) | 2024.03.29 |