switch~case
switch~case 구문 변화
JDK 12에서는 switch~case 구문이 조금 더 직관적으로 변화하였다.
lambda 적용
변수에 삽입 가능
기존 switch 구문
switch(month){
case 3:
case 4:
case 5:
System.out.println("봄");
break;
case 6:
case 7:
case 8:
System.out.println("여름");
break;
case 9:
case 10:
case 11:
System.out.println("가을");
break;
case 12:
case 1:
case 2:
System.out.println("겨울");
break;
}
람다식이 적용된 switch 구문
switch(month){
case 3, 4, 5 -> System.out.println("봄");
case 6, 7, 8 -> System.out.println("여름");
case 9, 10, 11 -> System.out.println("가을");
case 12, 1, 2 -> System.out.println("겨울");
}
변수에 반환값을 저장하도록 작성된 switch 구문
String season = switch(month){
case 3, 4, 5 -> "봄";
case 6, 7, 8 -> "여름";
case 9, 10, 11 -> "가을";
case 12, 1, 2 -> "겨울";
}
Last updated