프로그래밍/JAVA

[JAVA] 자바 17 특징 - 텍스트 블록

lazy man 2023. 4. 30. 01:17

1. 기존 스타일과의 비교

String textBlockHtml = """
        <html>

            <body>
                <span>example text</span>
            </body>
        </html>""";
@Test
public void givenAnOldStyleMultilineString_whenComparing_thenEqualsTextBlock() {
    String expected = "<html>\n"
            + "\n"
            + "    <body>\n"
            + "        <span>example text</span>\n"
            + "    </body>\n"
            + "</html>";
    assertEquals(expected, textBlockHtml);
}
    
    
@Test
public void givenAnOldStyleString_whenComparing_thenEqualsTextBlock() {
    String expected = "<html>\n\n    <body>\n        <span>example text</span>\n    </body>\n</html>";
    assertEquals(expected, textBlockHtml);
}

 

 

2. 들여쓰기

String textBlockIntent = """
                Indent
            """;
@Test
public void givenAnIndentedString_thenMatchesIndentedOldStyle() {
    String expected = "    Indent\n";
    assertEquals(expected, textBlockIntent);
}

 

 

3. 이스케이프, 캐리지리턴

- """을 제외한 이스케이프 문자를 표현하기 위해 \ 를 사용하지 않아도 된다. 그리고 개행할 때 \r\n(윈도우 기반) 이더라도 \n으로만 인식하기 때문에 캐리지리턴을 직접 표시해줘야한다.

String textBlockEscapes = """
            "fun" with
            whitespace
            and other escapes \"""
            """;
@Test
public void givenAnEscapesString_thenMatchesEscapesOldStyle() {
    String expected = "\"fun\" with\n"
            +"whitespace\n"
            +"and other escapes \"\"\""
            +"\n";

    assertEquals(expected, textBlockEscapes);
}


String textBlockCarriageReturns = """
            separated with\r
            carriage returns""";
@Test
void givenATextWithCarriageReturns_thenItContainsBoth(){
    String expected = "separated with\r\ncarriage returns";
    assertEquals(expected, textBlockCarriageReturns);
}

 

 

4. 개행 무시

- \ 를 사용하면 개행을 무시할 수 있다

String textBlockIgnoredNewLines = """
            This is a long test which looks to \
            have a newline but actually does not""";
@Test
void givenAStringWithEscapedNewLines_thenTheResultHasNoNewLines() {
    String expected = "This is a long test which looks to have a newline but actually does not";
    assertEquals(expected, textBlockIgnoredNewLines);
}

 

 

5. 빈 공간

- 컴파일러는 문자열의 후행 공백을 무시하지만, \s를 사용하면 공백을 유지할 수 있다.

String textBlockEscapedSpaces = """
            line 1       
            line 2       \s
            """;
@Test
void givenAStringWithEscapesSpaces_thenTheResultHasLinesEndingWithSpaces() {
    String expected = "line 1\nline 2        \n";
    assertEquals(expected, textBlockEscapedSpaces);
}

 

 

6. 문자열 포맷

String textBlockFormatting = """
            Some parameter: %s
            """.formatted("troh");
@Test
void givenAStringWithFormatted_thenResultHasParameter() {
    String expected = "Some parameter: troh\n";
    assertEquals(expected, textBlockFormatting);
}

 

 


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

2023.05.01 - [기타/프로그래밍 언어] - [JAVA] 자바 17 특징 - 기타

 

[JAVA] 자바 17 특징 - 기타

이번 포스팅에서는 자바 17의 특징 중 텍스트 블록, Switch 문, Record, Sealed 외의 특징에 대해 알아보도록 하겠습니다. 1. instanceof instanceof 체크할 때 변수를 선언할 수 있기 때문에 좀 더 간결한 코드

lazy-man.tistory.com