파란하늘의 지식창고
article thumbnail
Published 2018. 11. 21. 18:16
JDK 9 New Features Study/Java
반응형

JDK의 버전별 변경 사항은 이 곳을 참고하세요.

특징과 일정은 JEP 2.0 draft에 의해 개정된 JEP 프로세스를 통해 관리된다.

Spec

Java SE 9 Platform Umbrella JSR (379) 에 정의된 바와 같이 JSR 379 구현이 목표

실제 Spec은 Final Release Specification 문서를 참고해야함

Final Release Specification Feature summary

전체 JEP Feature 목록은 OpenJDK의 JDK 9 문서로 확인 할 수 있다.

Component

Feature

 

 Core Libraries

Convenience Factory Methods for Collections

불변 컬렉션 생성 편의성 메소드 제공

Enhanced Deprecation

@Deprecated에 좀 더 자세한 설명 기능 추가 (since, forRemoval 추가)

Enhanced Method Handles

 

Filter Incoming Serialization Data

JEP 추가 Feature

Indify String Concatenation

JEP 추가 Feature, String concat 처리 개선

More Concurrency Updates

CompletableFuture API 개선 및 기타 다양한 개선

Platform Logging API and Service

 

Process API Updates

운영 체제 프로세스를 제어 및 관리하기위한 API 개선

Spin-Wait Hints

스핀 루프 확인을 위한 onSpinWait() 제공

Stack-Walking API

JEP 추가 Feature, 스택 트레이싱을 쉽게 필터링, 지연 엑세스 할 수 있는 표준 API

UTF-8 Property Resource Bundles

JEP 추가 Feature, ResourceBundle 호출이 드디어 ISO-8859-1 에서 UTF-8로 전환됨

Use CLDR Locale Data by Default

JEP 추가 Feature

Variable Handles

 

Security

Create PKCS12 Keystores by Default

 

DRBG-Based SecureRandom Implementations

 

Datagram Transport Layer Security (DTLS)

 

OCSP Stapling for TLS

 

TLS Application-Layer Protocol Negotiation Extension

 

Client Libraries

Deprecate the Applet API

Applet API 제거 선언 추가

Multi-Resolution Images

다중 해상도 이미지 API 추가

Platform-Specific Desktop Features

 

TIFF Image I/O

TIFF 이미지 

Language

Elide Deprecation Warnings on Import Statements

 

Milling Project Coin

JDK 7에 적용된 Coin Project 추가 개선

Platform

Module System (JSR 376)

모듈 시스템 

Multi-Release JAR Files

다중 릴리스 jar 파일 

New Version-String Scheme

새 버전 문자열 체계

Unicode 8.0

JEP 추가 Feature

XML

XML Catalogs

 

JSR 379 Feature 중 Local-Variable Type Inference 는 JDK 10 에 반영되었다.

Core Libraries Feature

Convenience Factory Methods for Collections

컬렉션을 위한 편의성 팩토리 메소드를 제공한다.

기존 방식으로 불변 Colleciton을 만드는 방법은 다음과 같다.

Set<String> set = new HashSet<>();
set.add("a");
set.add("b");
set.add("c");
set = Collections.unmodifiableSet(set);

// 또 다른 방법 - 다른 Collection의 복사본 생성자를 사용
Set<String> set = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("a", "b", "c")));

// 또 다른 방법 - double brace 를 사용
Set<String> set = Collections.unmodifiableSet(new HashSet<String>() {{
    add("a"); add("b"); add("c");
}});
// 또 다른 방법 - Java 8 Stream API 사용
Set<String> set = Collections.unmodifiableSet(Stream.of("a", "b", "c").collect(toSet()));

팩토리 메소드를 사용하면 다음과 같다.

Set<String> set = Set.of("a", "b", "c");
List<String> list = List.of("a", "b", "c");

List와 Set의 of 메소드는 다음과 같다.

추가하는 컬렉션의 크기에 제한은 없으나 10개 이내인 경우는 초기화와 가비지 컬렉션 오버헤드를 피할 수 있도록 컬렉션 크기를 고정해준다.

Map의 경우 key/value를 순차로 입력받는 형태로 10개 이내까지 지원한다.

Map<String, String> map = Map.of("key1", "a", "key2", "b", "key3", "c");

10개를 초과한 entry를 추가하려면 Map.ofEntries(Map.Entry...) 를 사용해서 추가해야한다.

Language Feature

Milling Project Coin

coin 프로젝트는 JDK 7 에서 제공된 좀더 편리한 코딩을 위한 기능 개선을 위한 프로젝트였음.

이 개선점에 대해 추가 개선 사항이 있어 추가된 JEP

Allow @SafeVarargs on private methods

private method에 @SafeVarargs 를 사용할 수 있게 되었다.

Allow effectively-final variables to be used as resources in the try-with-resources statement

try-with-resource는 try 구문의 () 안에 선언된 AutoCloseable를 상속받은 자원 객체의 close method를 블록이 종료될 때 자동으로 호출하게 하는 기능이다.

JDK 7에 추가된 기능으로 다음과 같이 사용한다

// JDK 7 이전 자원 종료 처리 방식
BufferedReader br = null;
try {
	br = new BufferedReader(new FileReader("C:\\someText.txt"));
	// do something
} catch (IOException e) {
	e.printStackTrace();
} finally {
	if (br != null) {
		try {
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	} 
}

// JDK 7 이후 try-with-resources를 사용한 자원 종료 처리 방식
try (BufferedReader br = new BufferedReader(new FileReader("C:\\someText.txt"))) {
	// do something
} catch (IOException e) {
	e.printStackTrace();
}

close 메소드 호출이 보장되기 때문에 try 구문 이전에 변수 선언 후 finally 블럭에서 해당 변수의 close 처리를 하는 복잡한 반복성 코딩이 많이 줄어들게 되었다.

하지만 간혹 try 구문 밖에서 AutoCloseable 을 상속받은 자원 객체를 선언하고 close 처리를 보장받기 위해서는 다음과 같은 형태로 사용해야했다.

BufferedReader br = new BufferedReader(new FileReader("C:\\someText.txt"));
try (BufferedReader br2 = br) {
	// do something
} catch (IOException e) {
	e.printStackTrace();
}

try 구문 밖에서 br에 대해 어떤 처리를 하려는 경우 구문 밖에서 지정해서 사용하고 try-with-resource 구문을 통한 close 처리를 보장하기 위해 try 구문의 () 안에서 한번 더 참조된 변수를 선언해야만 하는 불편함이 있었다.

JDK 9에서는 다음과 같이 사용할 수 있다.

BufferedReader br = new BufferedReader(new FileReader("C:\\someText.txt"));
try (br) {
	// do something
} catch (IOException e) {
	e.printStackTrace();
}

Allow diamond with anonymous classes if the argument type of the inferred type is denotable

다이아몬드 연산자가 익명 클래스와 함께 사용할 수 있도록 변경되었다.

아래처럼 왼쪽에서 타입을 유추할 수 있으면 오른쪽에서는 다이아몬드 연산자를 사용하여 타입 선언을 생략할 수 있는 기능이다.

Map<String, String> map1 = new HashMap<String, String>();	// JDK 7 이전 방식
Map<String, String> map2 = new HashMap<>();	// JDK 7 이후 다이아몬드 연산자로 간편해진 방식

JDK 9 이전엔 익명 클래스에서 다이아몬드 연산자를 사용하지 못하였다.

Iterator<String> iter = new Iterator<>() { /* ... */ };	// 에러 발생

JDK 9 이후 아래와 같이 사용이 가능하게 된다.

Iterator<String> iter = new Iterator<>() {

	@Override
	public boolean hasNext() {
		// do something
		return false;
	}

	@Override
	public String next() {
		// do something
		return null;
	}
	
};

Complete the removal, begun in Java SE 8, of underscore from the set of legal identifier names

_ 가 기존엔 변수명으로 사용이 가능하였지만 예약어로 지정되어 사용하지 못하게 된다.

해당 예약어는 JEP 302 (Lambda Leftovers) 에서 사용될 예정이다.

Support for private methods in interfaces

JDK 8에 추가된 default method의 코드의 재사용성을 위한 private 메소드도 JDK 9 에서 지원하게 되었다.

private method는 내부에서 사용하기 위한 메소드이기 때문에 구현체여야 하며 abstract나 default 키워드를 같이 사용할 수 없다.

public interface TestInterface {

	default String testMethod() {
		return testPrivateMethod();
	}
	
	private String testPrivateMethod() {
		return "privateMethodCall";
	}
	
	private default String testPrivateMethod2() {	// 이렇게 사용 불가
		return "privateMethod2Call";
	}
	
	private abstract String testPrivateMethod3();	// 이렇게 사용 불가

}

Platform feature

Module System (JSR 376)

JDK 9 이후 Jigsaw Project의 모듈 기반 관리가 도입됨


반응형
profile

파란하늘의 지식창고

@Bluesky_

내용이 유익했다면 광고 배너를 클릭 해주세요