자바
[자바] Java 11 버전 특징과 주요 변경 사항
kyjdummy
2025. 5. 7. 20:57
[ 자바 11의 지원 일자 ]
- 무료 지원 일자 : 2018-09 ~ 2023-09
- 유료지원 일자 : 2032-01
[ 호환 스프링 부트 ]
- Spring Boot 호환 : 2.1.x ~ 2.7.x(3.x 부터는 17 이상 필요)
- 안정 적인 버전 : 자바 11과 2.7.x(18)이 안정적
[ 가비지 컬렉터 ]
- 자바 11에서는 G1 가비지 컬렉터가 기본으로 사용되며, 새로운 가비지 컬렉터 ZGC(Z Garbage Collector)가 실험적으로 도입되었습니다.
[ 특징 및 주요 변경 사항 ]
1. String 클래스의 새로운 메소드들
- isBlank()
- 문자열이 비어있거나 공백 문자(스페이스, 탭, 개행 등) 로만 이루어져 있는지 확인하는 메소드입니다. 기존 isEmpty() 함수는 공백 문자만 포함된 문자열을 감지하지 못했습니다.
public class StringIsBlankExample {
public static void main(String[] args) {
// 비어있는 문자열
String emptyString = "";
System.out.println(emptyString.isBlank()); // true
// 공백 문자만 포함된 문자열
String whitespacesString = " \t \n";
System.out.println(whitespacesString.isBlank()); // true
// 일반 문자열
String normalString = "Java 11";
System.out.println(normalString.isBlank()); // false
// isBlank()와 isEmpty()의 차이점
System.out.println("공백 문자열 is empty? " + whitespacesString.isEmpty()); // false
System.out.println("공백 문자열 trim 후 is empty? " + whitespacesString.trim().isEmpty()); // true
}
}
- lines()
- 문자열을 라인 단위로 분리하여 Stream 객체로 반환합니다.
public class StringLinesExample {
public static void main(String[] args) {
String multilineString = "첫 번째 라인\n두 번째 라인\r\n세 번째 라인";
// lines() 메소드로 라인별 처리
multilineString.lines()
.map(line -> "라인: " + line)
.forEach(System.out::println);
// 라인 수 계산
long lineCount = multilineString.lines().count();
System.out.println("총 라인 수: " + lineCount); // 3
}
}
- 공백 제거 문자열 함수
- strip(), stripLeading(), stripTrailing() 함수가 추가되었습니다. 기존 trim() 메소드는 Unicode 공백을 제대로 처리하지 못하는 한계가 있었습니다.
public class StringStripExample {
public static void main(String[] args) {
String text = " Java 11 신기능 ";
// strip(): 앞뒤 공백 모두 제거
System.out.println("strip(): " + text.strip());
// stripLeading(): 앞쪽 공백만 제거
System.out.println("stripLeading(): " + text.stripLeading());
// stripTrailing(): 뒤쪽 공백만 제거
System.out.println("stripTrailing(): " + text.stripTrailing());
}
}
- repeat(int)
- 문자열을 지정된 횟수만큼 반복하여 새로운 문자열을 생성합니다.
public class StringRepeatExample {
public static void main(String[] args) {
String star = "*";
// 별표 10개 반복
System.out.println(star.repeat(10)); // **********
}
}
2. 파일 처리 관련 기능
- Files.readString()과 Files.writeString() 함수가 추가되었습니다. Files 클래스에 추가된 정적 메소드로, 파일 내용을 읽고 쓰는 작업을 간단하게 처리할 수 있습니다.
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.nio.charset.StandardCharsets;
import java.io.IOException;
public class FilesReadWriteExample {
public static void main(String[] args) {
try {
// 파일에 내용 쓰기
Path filePath = Path.of("example.txt");
Files.writeString(filePath, "Java 11의 새로운 파일 API 테스트입니다.", StandardCharsets.UTF_8);
System.out.println("파일에 내용을 작성했습니다.");
// 파일 내용 읽기
String content = Files.readString(filePath, StandardCharsets.UTF_8);
System.out.println("파일 내용: " + content);
// 파일에 내용 추가
Files.writeString(
filePath,
"\n이 내용은 파일에 추가됩니다.",
StandardCharsets.UTF_8,
StandardOpenOption.APPEND
);
// 업데이트된 내용 읽기
content = Files.readString(filePath, StandardCharsets.UTF_8);
System.out.println("업데이트된 파일 내용: " + content);
} catch (IOException e) {
System.err.println("파일 처리 중 오류 발생: " + e.getMessage());
}
}
}
3. 컬렉션 개선
- 컬렉션의 toArray() 메소드에 새로운 오버로딩이 추가되었습니다. 기존 toArray는 사용이 번거로웠고, 타입 안정성이 부족했습니다.
import java.util.List;
import java.util.Arrays;
public class ToArrayExample {
public static void main(String[] args) {
List<String> list = List.of("Java", "Python", "JavaScript");
// 기존 방식
String[] oldArray = list.toArray(new String[0]);
System.out.println("기존 방식: " + Arrays.toString(oldArray));
// 새로운 방식
String[] newArray = list.toArray(String[]::new);
System.out.println("새로운 방식: " + Arrays.toString(newArray));
// 다른 타입의 배열
List<Integer> numbers = List.of(1, 2, 3, 4, 5);
Integer[] numbersArray = numbers.toArray(Integer[]::new);
System.out.println("숫자 배열: " + Arrays.toString(numbersArray));
}
}
4. 람다 표현식 개선
- 자바 11에서는 람다 표현식의 매개변수에 var 키워드를 사용할 수 있게 되었습니다.
import java.util.function.Consumer;
public class LambdaVarExample {
public static void main(String[] args) {
// 기존 방식
Consumer<String> oldConsumer = (String s) -> System.out.println(s.length());
// var 사용
Consumer<String> newConsumer = (var s) -> System.out.println(s.length());
// 타입 어노테이션 사용
Consumer<String> annotatedConsumer = (@NonNull var s) -> System.out.println(s.length());
// 실행
oldConsumer.accept("Java 11");
newConsumer.accept("Java 11");
annotatedConsumer.accept("Java 11");
}
}
5. HTTP 클라이언트 표준화
- 자바 11에서는 자바 9에서 도입된 인큐베이터 모듈이었던 HTTP 클라이언트 API가 표준화되었습니다.
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.URI;
import java.io.IOException;
public class HttpClientExample {
public static void main(String[] args) {
try {
// HttpClient 생성
HttpClient client = HttpClient.newHttpClient();
// HTTP 요청 생성
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.example.com/data"))
.header("Content-Type", "application/json")
.GET()
.build();
// 동기 요청 보내기
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
// 응답 처리
System.out.println("상태 코드: " + response.statusCode());
System.out.println("응답 헤더: " + response.headers());
System.out.println("응답 본문: " + response.body());
// 비동기 요청 예시
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.join(); // 완료까지 대기
} catch (IOException | InterruptedException e) {
System.err.println("HTTP 요청 중 오류 발생: " + e.getMessage());
}
}
}
암호화 알고리즘 변경
- 현대적인 암호화 요구사항 반영과 양자 컴퓨팅 시대에 대비한 보다 강력한 알고리즘 도입을 위해 알고리즘이 추가, 변경되었습니다.
- 추가된 알고리즘:
- ChaCha20 및 Poly1305 암호화 알고리즘 추가
- SHA-3 해시 알고리즘 패밀리 추가 (SHA3-224, SHA3-256, SHA3-384, SHA3-512)
- 제거/비 권장된 알고리즘:
- RSA 키 생성에서 1024비트 키 크기 비활성화 (기본적으로 사용 불가)
- SHA-1 해시 알고리즘의 추가 사용 제한
- 여러 약한 암호화 스위트 비활성화 (DES, RC4 등)