2016. 12. 7. 12:23 java
java7,8 신규 기능
자바6만 사용하다 보니 7,8에 대한 정리가 필요할거 같아서... 공부 하면서 작성해 봄
http://www.jpstory.net/2014/06/java-7-features/
Java 7
1 Type Inference
생성자 영역의 타입 파라미터들은 <> 로 대체 가능
List<Integer> list = new ArrayList<>();
2 String in Switch
switch 문에서 문자열 사용이 가능
3 Automatic Resource Management
try with resource 구문이 추가되어 자동으로 resource들을 close 해줍니다.
AutoClosable, Closeable 인터페이스를 구현한 경우 try(resource)내의 resource들에 대해
close()를 수행.
기본적으로 Java7의 Streams, Files, Socket, DB Connection 등은 해당 인터페이스를 구현
try
(FileInputStream fin =
new
FileInputStream(
"info.xml"
);
BufferedReader br =
new
BufferedReader(
new
InputStreamReader(
fin));) {
if
(br.ready()) {
String line1 = br.readLine();
System.out.println(line1);
}
}
catch
(FileNotFoundException ex) {
System.out.println(
"Info.xml is not found"
);
}
catch
(IOException ex) {
System.out.println(
"Can't read the file"
);
}
4 Fork/Join Framework
5 Underscore in Numeric literal
숫자형(정수, 실수)에 _(underscore) 문자열을 사용할 수 있음
int
billion = 1_000_000_000;
6 Catching Multiple Exception Type in Single Catch Block
try {
//......
} catch (ClassNotFoundException|SQLException ex) {
ex.printStackTrace();
}
Multi-Catch 구문 사용시 다음과 같이 하위클래스 관계라면 컴파일 에러가 발생하므로 주의
7 Binary Literals with Prefix “0b”
숫자형에 ‘0B’ 또는 ‘0b’를 앞에 붙임으로써 이진법 표현이 가능합니다. (8진법은 ‘0’, 16진법은 ‘0X’ 또는 ‘0x’)
int
mask = 0b01010000101;
int
binary = 0B0101_0000_1010_0010_1101_0000_1010_0010;
// _를 이용한 가독성 향상
8
java NIO 2.09 G1 Garbage Collector
10 More Precise Rethrowing of Exception
11 1. Null-safe Method invocaton
return person?.getAddress()?.getPostcode();
값이 null 이면 null을 리턴하고 아니면 변수값을 리턴
12 4. Bracket Notation for Collection
선언과 동시에 초기화
Collection<String> collection = new ArrayList {"one", "two", "three"};
13 MethodHandles
java8
1 참조 투명성
함수형 언어
2 인터페이스
상수 필드, 추상 메소드, 디폴트 메소드, 정적 메소드
3 인터페이스 구현
익명 구현 객체
Person person = new Person() {
public void speak() {
}
}
다중 인터페이스 구현 클래스
4 타입 변환
자동 타입 변환, 강제 타입 변환, 객체 타입 변환
5 인터페이스 상속
다중 상속 가능
'java' 카테고리의 다른 글
[ 링크 ] 대용량 파일을 AsynchronousFileChannel로 다뤄보기 (0) | 2018.02.19 |
---|---|
메모리 분석 (0) | 2016.12.13 |
radius (0) | 2015.09.11 |
세션 타임아웃 관련 처리 (0) | 2015.07.01 |
SecureRandom 지연이슈 (0) | 2015.03.19 |