이번 포스팅에서는 자바 17의 특징 중 텍스트 블록, Switch 문, Record, Sealed 외의 특징에 대해 알아보도록 하겠습니다.
1. instanceof
instanceof 체크할 때 변수를 선언할 수 있기 때문에 좀 더 간결한 코드를 작성할 수 있습니다.
@Test
public void oldStyleInstanceof() {
Object o = new Person("troh", 33);
if(o instanceof Person) {
Person p = (Person) o;
Assertions.assertEquals("troh", p.getName());
Assertions.assertEquals(33, p.getAge());
}
}
@Test
public void newStyleInstanceof() {
Object o = new Person("troh", 33);
if(o instanceof Person p) {
Assertions.assertEquals("troh", p.getName());
Assertions.assertEquals(33, p.getAge());
}
}
2. 컴팩트한 숫자 포맷 지원(NumberFomat)
@Test
public void shortFormat() {
NumberFormat fmt1 = NumberFormat.getCompactNumberInstance(Locale.ENGLISH, NumberFormat.Style.SHORT);
Assertions.assertEquals("1K", fmt1.format(1000));
Assertions.assertEquals("10K", fmt1.format(10000));
Assertions.assertEquals("1M", fmt1.format(1000000));
NumberFormat fmt2 = NumberFormat.getCompactNumberInstance(Locale.KOREA, NumberFormat.Style.SHORT);
Assertions.assertEquals("1천", fmt2.format(1000));
Assertions.assertEquals("1만", fmt2.format(10000));
Assertions.assertEquals("100만", fmt2.format(1000000));
}
@Test
public void longFormat() {
NumberFormat fmt1 = NumberFormat.getCompactNumberInstance(Locale.ENGLISH, NumberFormat.Style.LONG);
Assertions.assertEquals("1 thousand", fmt1.format(1000));
Assertions.assertEquals("10 thousand", fmt1.format(10000));
Assertions.assertEquals("1 million", fmt1.format(1000000));
NumberFormat fmt2 = NumberFormat.getCompactNumberInstance(Locale.KOREA, NumberFormat.Style.LONG);
Assertions.assertEquals("1천", fmt2.format(1000));
Assertions.assertEquals("1만", fmt2.format(10000));
Assertions.assertEquals("100만", fmt2.format(1000000));
}
3. 기간지원(DateTimeFormatter - Pattern B 추가)
@Test
public void patternB() {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("B");
Assertions.assertEquals("오전", dtf.format(LocalTime.of(8, 0)));
Assertions.assertEquals("오후", dtf.format(LocalTime.of(13, 0)));
Assertions.assertEquals("저녁", dtf.format(LocalTime.of(20, 0)));
Assertions.assertEquals("밤", dtf.format(LocalTime.of(23, 0)));
Assertions.assertEquals("자정", dtf.format(LocalTime.of(0, 0)));
}
4. Stream.toList() 개선
- 과거의 Collector를 이용해서 List로 변환하던 방식이 toList() 메소드를 호출하는 방식으로 변경되었습니다.
@Test
public void oldStyleToList() {
Stream<String> stringStream = Stream.of("a", "b", "c");
List<String> stringList = stringStream.collect(Collectors.toList());
}
@Test
public void newStyleToList() {
Stream<String> stringStream = Stream.of("a", "b", "c");
List<String> stringList = stringStream.toList();
}
2023.04.30 - [기타/프로그래밍 언어] - [JAVA] 자바 17 특징 - 텍스트 블록
[JAVA] 자바 17 특징 - 텍스트 블록
1. 기존 스타일과의 비교 String textBlockHtml = """ example text """; @Test public void givenAnOldStyleMultilineString_whenComparing_thenEqualsTextBlock() { String expected = "\n" + "\n" + " \n" + " example text\n" + " \n" + ""; assertEquals(expect
lazy-man.tistory.com
2023.05.01 - [기타/프로그래밍 언어] - [JAVA] 자바 17 특징 - Switch 문
[JAVA] 자바 17 특징 - Switch 문
자바 17에서는 Switch 문의 기능이 개선되었다. 향상된 Switch 문을 사용하면 break 문을 사용하거나 여러 개의 case를 묶을 때 자바 11 이전의 버전보다 편리하게 사용할 수 있다. 1. 자바 11 이전의 switch
lazy-man.tistory.com
2023.05.01 - [기타/프로그래밍 언어] - [JAVA] 자바 17 특징 - Record Type
[JAVA] 자바 17 특징 - Record Type
불변성을 가지는 데이터 클래스를 위한 타입이다. 자바 14 이전 버전에서는 불변의 데이터 클래스를 생성하기 위해서는 private final 필드, 모든 필드를 초기화하는 생성자, equals 메서드, toString 메
lazy-man.tistory.com
2023.05.01 - [기타/프로그래밍 언어] - [JAVA] 자바 17 특징 - Sealed Class
[JAVA] 자바 17 특징 - Sealed Class
sealed class, interface는 상속하거나 구현할 클래스를 지정하고, 지정한 클래스 외에는 상속 또는 구현할 수 없도록 하는 기능으로 무분별한 상속과 구현을 방지할 수 있습니다. 1. sealed 클래스(인터
lazy-man.tistory.com
'프로그래밍 > JAVA' 카테고리의 다른 글
| [JAVA] 함수형 인터페이스 - Predicate (0) | 2023.12.13 |
|---|---|
| [JAVA] 자바 17 특징 - Sealed Class (0) | 2023.05.01 |
| [JAVA] 자바 17 특징 - Record Type (0) | 2023.05.01 |
| [JAVA] 자바 17 특징 - Switch 문 (0) | 2023.05.01 |
| [JAVA] 자바 17 특징 - 텍스트 블록 (0) | 2023.04.30 |