HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
🤩
개발
/
Java
Java
/
💯
Java 기본
/기본개념/
🎢
switch 문
🎢

switch 문

switch (inputText) { case "exit": return; case "create": switch (console.chooseVoucher()) { case 1 -> service.createVoucher(new FixedAmountVoucher(10L, UUID.randomUUID())); case 2 -> service.createVoucher(new PercentDiscountVoucher(10L, UUID.randomUUID())); default -> throw new IllegalArgumentException(); } break; case "list": service.listVoucher(); break; default: throw new IllegalArgumentException(); }
  • break문을 넣지 않으면 그 다음 조건도 실행하게 됨. default 가 맨 마지막에 존재하는데, case “list”에 break를 넣지 않게되면 default 문 실행함
  • lambda 형태로 case를 작성하게 되면 break 넣은 것과 동일하게 동작함