[Java] Refactor: switch statement can be replaced with enhanced 'switch'

2026. 2. 9. 16:54·Development/Java

개선된 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
'Development/Java' 카테고리의 다른 글
  • [Java] Refactor: Non-constant string concatenation as argument to logging call
  • [Java] Getter/Setter 지양과 trim 책임 분리 (feat.model 리팩토링)
  • [Java] 자바 기본 정리하기
곽진돔
곽진돔
Developer
  • 곽진돔
    echo "곽박한 세상";
    곽진돔
  • 전체
    오늘
    어제
    • 분류 전체보기 (217) N
      • Development (84) N
        • Linux (13)
        • k8s (3)
        • Docker (5)
        • AWS (1)
        • PHP (35)
        • Python (21)
        • Java (4)
        • SpringBoot (4)
        • JavaScript (2)
        • React (11)
        • MySql (19)
        • MongoDB (1)
      • Daily (6)
      • Study (7)
        • TIL (2)
        • license (3)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
    • 글쓰기
    • 설정
  • 링크

    • github
  • 공지사항

  • 인기 글

  • 태그

    nodejs
    Linux
    Mac
    springboot
    react
    SQL
    IP
    Java
    HTML
    리눅스
    리액트
    Selenium
    php
    Shell
    MySQL
    인코딩
    정규표현식
    docker
    db
    CentOS
    chromedriver
    GPT
    UTF8
    크롤링
    스프링부트
    CentOS7
    ssh
    AI
    JavaScript
    Python
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.6
곽진돔
[Java] Refactor: switch statement can be replaced with enhanced 'switch'
상단으로

티스토리툴바