๐๏ธ ์์ ๋ฅผ ํฌํจํ Java 21 ๊ธฐ๋ฅ
์ด ๋ฌธ์์์๋ JDK 21์ ๋์ ๋ ์๋ก์ด ๊ธฐ๋ฅ์ ๋ํ ๊ฐ์๋ฅผ ์ ๊ณตํฉ๋๋ค. ์์ธํ ๋ด์ฉ์ ์ดํด๋ณด๊ณ JDK 21์ ํฅ๋ฏธ๋ก์ด ๋ฐ์ ์ ๋ํด ์์๋ด ์๋ค. ๐ String Templates (Preview) ์๋ฐ์์ ๋ฌธ์์ด ํฌ๋งท ๋ฐ ์กฐ์ ๊ณผ์ ์ ๋จ์ํํ๋ ๊ฒ์ ๋ชฉํ๋ก ํฉ๋๋ค. ์ด ๊ธฐ๋ฅ์ ํตํด ๋ณต์กํ ๋ฌธ์์ด์ ์์ฑํ๊ณ ํฌ๋งทํ ์ ์์ต๋๋ค. // As of Java 21 String productName = "Widget"; boolean productAvailable = true; String productInfo = `Product: ${productName} Availability: ${productAvailable ? "In Stock" : "Out of Stock"}`; System.out.println(productInfo); ๐ Sequenced Collections Sequenced Collection์ ๋์ ์ ์ปฌ๋ ์ ์ฒ๋ฆฌ๋ฅผ ๋จ์ํํ๊ณ ๋ฅ๋ฅ ํํ๊ธฐ ์ํ ์๋ก์ด ์ธํฐํ์ด์ค์ ๋ฉ์๋๋ฅผ ์ ๊ณตํฉ๋๋ค. ์ด ํฅ์๋ ๊ธฐ๋ฅ์ ์๋ฐ์์ ์ปฌ๋ ์ ์ ์ฒซ ๋ฒ์งธ ์์์ ๋ง์ง๋ง ์์์ ์ ๊ทผํ๋ ๊ธฐ์กด์ ๋ฒ๊ฑฐ๋ก์ด ์ ๊ทผ ๋ฐฉ์์ ํด๊ฒฐํ๋ ๊ฒ์ ๋ชฉํ๋ก ํฉ๋๋ค. For List, Deque, Set First Element โ collection.getFirst() Last Element โ collection.getLast() ๐ Record Patterns ๋ ์ฝ๋ ํจํด์ผ๋ก ๋ ์ฝ๋ ๊ฐ์ ์ฌ๊ตฌ์ฑํฉ๋๋ค. ๋ ์ฝ๋ ํจํด๊ณผ ํ์ ํจํด์ ์ค์ฒฉ์์ผ ๋ณตํฉ์ ์ธ ํํ์ ๋ฐ์ดํฐ ํ์ ๋ฐ ์ฒ๋ฆฌ๋ฅผ ๊ฐ๋ฅํ๊ฒ ํฉ๋๋ค. // As of Java 21 static void printSum(Object obj) { if (obj instanceof Point(int x, int y)) { System.out.println(x+y); } } ๐ Pattern Matching for switch ์ค์์น ๋ฌธ์ ๋ํ ํจํด ๋งค์นญ์ผ๋ก ์๋ฐ ์ธ์ด๋ฅผ ์ ๊ทธ๋ ์ด๋ํฉ๋๋ค. ์ฌ๋ฌ ํจํฐ์ ๋ํด ์์ ํ ์คํธ ํ์ฌ ๋ณต์กํ ๋ฐ์ดํฐ ์ค์ฌ์ ํ๋ฅผ ๊ฐ๊ฒฐํ๊ณ ์์ ํ๊ฒ ํํ์ด ๊ฐ๋ฅํฉ๋๋ค. // As of Java 21 static String formatterPatternSwitch(Object obj) { return switch (obj) { case Integer i -> String.format("int %d", i); case Long l -> String.format("long %d", l); case Double d -> String.format("double %f", d); case String s -> String.format("String %s", s); default -> obj.toString(); }; }