목차
JDK 17
2021.09.14 (예정)
신규기능
- Sealed Classes
상속 가능한 클래스를 제한(봉인) 할 수 있는 클래스 제공
public abstract sealed class Shape
permits Circle, Rectangle, Square { ... }
Shape rotate(Shape shape, double angle) {
return switch (shape) { // pattern matching switch
case Circle c -> c;
case Rectangle r -> shape.rotate(angle);
case Square s -> shape.rotate(angle);
// no default needed!
}
}
JDK 16
2021.03.16
신규 기능
- Records
immutable VO를 위한 새로운 타입
// old
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);
}
}
// new
record Point(int x, int y) {
// 필드, 생성자, getter 등이 암시적으로 자동 선언됨
}
- Pattern Matching for instanceof
instanceof 연산자의 패턴일치를 통한 프로그래밍 기능 향상
// old
if (obj instanceof String) {
String s = (String) obj; // 장황한 코드 발생
...
}
// new
if (obj instanceof String s) {
// 패턴매칭을 통해 추가적인 캐스팅없이 바로 사용가능
...
}
- local enums
private void localEnum(List<Person> people) {
// 매소드 내에서 enum 정의 가능
enum Gender {MALE, FEMALE};
Map<Gender, List<Person>> peopleByGender = new HashMap<>();
people.stream().filter(Person::isMale)
.forEach(person -> peopleByGender.computeIfAbsent(Gender.MALE, gender -> new ArrayList<>())
.add(person));
// do some more logic...
}
- local interfaces
private void localInterfaces() {
// 메소드 내에서 interface 정의 가능
interface Car {
int getModelNumber();
String getDescription();
}
class SuvCar implements Car {
@Override
public int getModelNumber() {
return 0;
}
@Override
public String getDescription() {
return null;
}
}
}
실험기능
- Sealed Classes (Second Preview)
⏰ JDK 17 Standard 적용예정
비고
- OpenJDK Community repositories가 Mecurial 에서 Git으로 변경
JDK 15
2020.09.15
신규 기능
- Text Blocks
여러행의 문자열 지원
// old
private String text() {
return "<html>\n"
+ "\t<body>\n"
+ "\t\t<h1>OLD JAVA!</h1>\n"
+ "\t</body>"
+ "</html>";
}
// new
private String textBlock() {
return """
<html>
<body>
<h1>JAVA15!</h1>
</body>
</html>
""";
}
실험기능
- Sealed Classes (Preview)
⏰ JDK 17 Standard 적용예정
- Records (Second Preview)
⏰ JDK 16 Standard 적용
- Pattern Matching for instanceOf (Second Preview)
⏰ JDK 16 Standard 적용
JDK 14
2020.03.17
신규기능
- Switch Expressions
Switch를 확장하여 값이나 표현식으로 사용가능
// Switch Expressions
int numLetters = switch (day) {
case MONDAY, FRIDAY, SUNDAY -> 6;
case TUESDAY -> 7;
case THURSDAY, SATURDAY -> 8;
case WEDNESDAY -> 9;
};
// Yielding a value
int result = switch (s) {
case "Foo":
yield 1;
case "Bar":
yield 2;
default:
System.out.println("Neither Foo nor Bar, hmmm...");
yield 0;
};
실험기능
- Records (Preview)
⏰ JDK 16 Standard 적용
- Pattern Matching for instanceOf (Preview)
⏰ JDK 16 Standard 적용
- Text Blocks (Second Preview)
⏰ JDK 15 Standard 적용
비고
- Helpful NullPointerExceptions
NPE에 대한 상세한 예외사항 전달
JDK 13
2019.09.17
신규기능
- 신규 기능 없음
실험기능
- Switch Expressions (Preview)
JDK 12에서 프리뷰 했었는데 13에서도 프리뷰로 구분되어있음..
⏰ JDK 14 Standard 적용
- Text Blocks (Preview)
여러행의 문자열 지원
⏰ JDK 15 Standard 적용
JDK 12
2019.03.19
신규기능
- 신규 기능 없음
실험기능
- Switch Expressions (Preview)
⏰ JDK 14 Standard 적용
JDK 11
2018.09.25
신규기능
- Local Variable Syntax for Lambda Parameters
비고
- Epsilon Garbage Collector
- HTTP Client
- Oracle JDK 독점기능 OpenJDK에 이식
JDK 10
2018.03.20
신규기능
- Local Variable Type Interface
특정 유형을 지정하는 대신
var
를 이용해 변수를 선언하고, 초기화 가능이미 대부분의 다른 언어에서 사용되고 있음 (C#의 경우 2007년 부터...)
// old
String greeting = "Hello World";
ArrayList<String> messages = new ArrayList<String>();
messages.add(greeting);
Stream<String> stream = messages.stream();
// new
var greeting = "Hello World";
var messages = new ArrayList<String>();
messages.add(greeting);
var stream = messages.stream();
비고
- Parallel Full GC for G1
전체 GC를 병렬처리하여, G1의 시간지연 케이스 개선
- 이전 버전에서
deprecated
된 method 모두 삭제
JDK 9
2018.09.21
신규기능
- Private Interface Method
- Convenience Factory Methods for Collections
컬랙션 생성을 위한 팩토리 메서드 제공
// old
Set<String> set = new HashSet<>();
set.add("a");
set.add("b");
set.add("c");
// new
Set<String> set = Set.of("a", "b", "c");
비고
- Module System
Project jigsaw 모듈시스템 개선
- jshell
- UTF-8 Property Files
deprecated
된 method 일부 삭제
JDK 8
2014.03.18
신규기능
- Lambda Expressions & Virtual Extension Methods
메서드와 유사하지만 이름이 필요없고, 메서드 본문에 바로 구현 가능한 짧은 코드 블록
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(5);
numbers.add(9);
numbers.add(8);
numbers.add(1);
Consumer<Integer> method = (n) -> { System.out.println(n); };
numbers.forEach( method );
}
- Annotations on Java Types
Type에 Annotaion 지정 가능
@Encrypted String data;
List<@NonNull String> strings;
- Default, Static Method Interface
인터페이스에서 일부 구현을 허용
다중상속의 문제 발생
- Optional
null을 포함 할 수도 있는 개체
private Optional<Person> findByName(String name) {
// null 이 반환 될 수 도 있는 경우
return Optional.of(new Person(20, name));
}
private void ExampleOptional(String name) {
Optional<Person> person = findByName(name);
if (person.isPresent()) {
System.out.println(person.get());
} else {
System.out.println("null value");
}
}
비고
- Date & Time API 변경
- Stream API 추가
JDK 7
2011.07.28
신규기능
- Diamond Operator
generics 타입 추론
// old
Map<Integer, String> map = new HashMap<Integer, String>();
// new
Map<Integer, String> map = new HashMap<>();
- try-resource
try 사용 시 finally에서 resource close 자동 처리
// old
public static void main(String args[]) throws IOException {
FileInputStream is = null;
BufferedInputStream bis = null;
try {
is = new FileInputStream("file.txt");
bis = new BufferedInputStream(is);
int data = -1;
while((data = bis.read()) != -1){
System.out.print((char) data);
}
} finally {
// close resources
if (is != null) is.close();
if (bis != null) bis.close();
}
}
// new
public static void main(String args[]) {
try (
FileInputStream is = new FileInputStream("file.txt");
BufferedInputStream bis = new BufferedInputStream(is)
) {
int data = -1;
while ((data = bis.read()) != -1) {
System.out.print((char) data);
}
} catch (IOException e) {
e.printStackTrace();
}
}
- Catching Multiple Exception Type ins Single Catch Block
try-catch 이 catch에 여러 예외를 한번에 처리 가능
// old
try {
// ...
} catch(SQLException e) {
logger.log(e);
} catch(IOException e) {
logger.log(e);
} catch(Exception e) {
logger.severe(e);
}
// new
try {
// ...
} catch(SQLException | IOException e) {
logger.log(e);
} catch(Exception e) {
logger.severe(e);
}
- String in Switch
JDK6까지 Primitive, Enum만 사용 가능
- Undersocre in Numberic
가독성을 위해 숫자자료형 변수에
_
허용
int amount = 1000000;
int amount = 1_000_000;
댓글