기본적으로 자바는 JAVA 9 이후부터 6개월에 한 번씩 3월과 9월에 새로운 릴리즈 버전을 내고 있다.
이 글을 작성하고 있는 2024.08.10 기준 JAVA SE 23 프리뷰까지 공개되었으며 9월에 최종 릴리즈가 나올 예정이다.
이 정보는 Oracle 자바 SE 로드맵을 통해 확인할 수 있다.
이번 글에서는 주요 변화가 있는 자바 버전의 기능을 간략하게 정리해본다.
자바 버전에는 non-LTS와 LTS 버전이 있다.
LTS 버전은 장기간동안 안정적인 보안 패치와 성능 개선 같은 지원을 받을 수 있다. 그렇기에 LTS 버전을 사용하는 게 일반적이다.
// Before
public class Box {
private Object object;
public void set(Object object) { this.object = object; }
public Object get() { return object; }
}
// After
public class Box<T> {
private T value;
public void set(T value) { this.value = value; }
public T get() { return value; }
}
enum Color {
RED("빨강"),
BLUE("파랑"),
BLACK("검정")
private String name;
Color(String name) { this.name = name; }
}
JAVA 8의 핵심은 함수형 프로그래밍 패러다임의 접목이다.
// Before
Comparator<Apple> weight = new Comparator<Apple>() {
public int compare(Apple a1, Apple a2) {
return a1.getWeight().compareTo(a2.getWeight());
}
}
// After
Comparator<Apple> weight = (Apple a1, Apple a2)
-> a1.getWeight().compareTo(a2.getWeight());
// Before
Fuction<String, Integer> stringToInt;
stringToInt = (s) -> Integer.parseInt(s);
stringToInt.apply("100");
// After
Function<String, Integer> stringToInt;
stringToInt = Integer::parseInt;
stringToInt.apply("100");
public interface MyInterface {
// regular interface methods
default void defaultMethod() {
// default method implementation
}
}
int sum = widgets.stream()
.filter(w -> w.getColor() == RED)
.mapToInt(w -> w.getWeight())
.sum();
NullPointerException을 방지하기 하기 쉬워짐Optional<String> notEmpty = Optional.of("Not Empty");
notEmpty.ifPresent(() -> System.out.println("It's Present");
| It's Present
Optional<String> empty = Optional.empty();
String orElseEmpty = empty.orElse("Default Value");
System.out.println(orElseEmpty);
| Default Value
var name = "Hong";
var age = 20;
strip(), stripLeading(), stripTrailing(), isBlank(), repeat(n) 메서드가 추가writeString(), readString(), isSameFile() 메서드 추가// Before
class Point {
private final int x;
private final int y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
int x() { return x; }
int y() { return y; }
public boolean equals(Object o) {
if (!(o instanceof Point)) return false;
Point other = (Point)o;
return other.x == x && other.y == y;
}
public int hashCode() {
return Objects.hash(x, y);
}
public String toString() {
return String.format("Point[x=%d, y=%d]", x, y);
}
}
// After
record Point(int x, int y) { }
sealed class Shape permits Circle, Rectangle, Square { ... }
public final class Circle extends Shape { ... }
public sealed class Rectangle extends Shape
permits TransparentRectangle { ... }
public non-sealed class Square extends Shape { ... }