스프링
[F-lab] 4주차 정리_자바
책 읽는 개발자_테드
2021. 9. 3. 10:41
반응형
숙제
1. 모듈시스템(10, jigsaw)
https://scshim.tistory.com/371
2. 스위치 확장(12)
자바 12버전 이전
ㆍswitch문을 통해 값을 반환할 수 없고, multiple case를 지원하지 않아 switch 문이 길고 지저분했다.
// Traditional switch
private static int getValueBefore12(String mode) {
int result;
switch (mode) {
case "a":
case "b":
result = 1;
break;
case "c":
result = 2;
break;
case "d":
case "e":
case "f":
result = 3;
break;
default:
result = -1;
}
;
return result;
}
자바 12버전 이후
1. 결과 값을 화살표 연산자(->)로 반환할 수 있고,
2. multiple case를 지원하는 switch 연산자가 추가되었다.
private static int getValueViaArrow(String mode) {
int result = switch (mode) {
case "a", "b" -> 1;
case "c" -> 2;
case "d", "e", "f" -> 3;
default -> -1;
};
return result;
}
자바 13버전 이후
ㆍ 값을 반환할 때 switch 연산자의 반환을 위해 yield 예약어를 사용할 수 있다.
private static int getValueViaYield(String mode) {
int result = switch (mode) {
case "a", "b":
yield 1;
case "c":
yield 2;
case "d", "e", "f":
// do something here...
System.out.println("Supports multi line block!");
yield 3;
default:
yield -1;
};
return result;
}
3. 레코드
https://scshim.tistory.com/372
4. 스트림
https://scshim.tistory.com/364
출처
https://velog.io/@nunddu/Java-Switch-Expression-in-Java-14
mkyong.com/java/java-13-switch-expressions/
https://blogs.oracle.com/javamagazine/new-switch-expressions-in-java-12
반응형