Java 11 Features with Examples

In this article, we will learn Java 11 features such as isBlank(), lines(), repeat(), strip() methods which are String related enhancements and for Optional types we will learn isEmpty() method. Let’s get started!

Java 11 Features

We will start with new methods which help us with some String operations. The first one is the isBlank() method.

isBlank() Method

The isBlank() method returns true if a string is empty or contains only white space characters. For other cases, it returns false. Let’s see this in the example below.

@Test
@Order(1)
public void isBlankTest() {
    String emptyText = "";
    String emptyTextWithSpace = "  ";

    System.out.println("emptyText isEmpty result: " + emptyText.isEmpty()); //true
    System.out.println("emptyText isBlank result: " + emptyText.isBlank()); //true

    System.out.println("emptyTextWithSpace isEmpty result: " + emptyTextWithSpace.isEmpty()); //false (because it contains spaces.)
    System.out.println("emptyTextWithSpace isBlank result: " + emptyTextWithSpace.isBlank()); //true
}

Output

isBlank() method in java 11

lines() Method

The lines() method returns a stream of lines extracted from the given string, separated by line terminators. In the example below, the text contains ‘\n’ new line characters, and the lines() method returns a stream of lines extracted from the example text.

First, the code prints the String variable text, and then we use the lines() method to convert this String text variable to a stream of lines, then by using the collect() method, we collect these lines as a list.

@Test
@Order(2)
public void stringLinesTest() {
    String text = "Hello\nSW Test Academy\nIt is a great site!\nGo and check!";
    System.out.println(text);

    var textList = text.lines() //lines() method creates a stream.
        .collect(Collectors.toList());
    System.out.println(textList);
    Assertions.assertEquals(4, textList.size());
}

Output

lines() method in java 11

repeat() Method

The repeat method returns a string whose value is the concatenation of this string repeated count times. If the given string is empty or the count is zero then it returns an empty string. The example below shows the repeat() method behavior.

@Test
@Order(3)
public void stringRepeatTest() {
    String text = "Let's repeat!";
    System.out.println(text.repeat(3));
}

Output

repeat() method in java 11

strip() Method

The strip() method returns a string with all leading and trailing white space removed. It removes all Unicode whitespace characters (but not all control characters, such as \0). This is the main difference between strip() and trim() methods.

@Test
@Order(4)
public void stringStripTest() {
    char c = '\u2002'; //space character.
    String text = c + " SW TEST ACADEMY ";

    System.out.println(text.trim()); //trim cannot remove unicode space character.
    System.out.println(text.strip()); //strip can remove unicode space character.
}

Output

strip() method

writeString() method in Files API

In Java 11, the Files API has the writeString() method which writes a character sequence to a file. Characters are encoded into bytes using the UTF-8 charset.

@SneakyThrows
@Test
@Order(5)
public void filesAPITest() {
    Path path = Paths.get(ClassLoader.getSystemResource("onur.txt").getPath());
    Files.writeString(path, "SW Test Academy", StandardOpenOption.WRITE, StandardOpenOption.SYNC);
    System.out.println("The Content: " + Files.readString(path) );
    System.out.println(Files.readString(path).contains("SW Test Academy"));
}

Output

Files API in java 11

Optional isEmpty() Method

isEmpty() method returns true if a value is not present otherwise it returns false. For other methods of the Optional Class, you can visit here. You can see the details in the examples below.

@Test
@Order(6)
public void optionalEmptyTest1() {
    var numbers = List.of(1, 2, 3, 4, 5, 6, 7);

    Optional<Integer> numbersGreaterThanFour = numbers.stream()
        .filter(number -> number > 4)
        .findFirst();

    //isPresent version
    if (numbersGreaterThanFour.isPresent()) {
        System.out.println("The number: " + numbersGreaterThanFour.get());
    } else {
        System.out.println("Number is not available!");
    }

    //isEmpty version
    if (numbersGreaterThanFour.isEmpty()) {
        System.out.println("Number is not available!");
    } else {
        System.out.println("The number: " + numbersGreaterThanFour.get());
    }
}

Output

isEmpty() in java

@Test
@Order(7)
public void optionalEmptyTest2() {
    var numbers = List.of(1, 2, 3, 4, 5, 6, 7);

    Optional<Integer> numbersGreaterThanSeven = numbers.stream()
        .filter(number -> number > 7)
        .findFirst();

    //isPresent version
    if (numbersGreaterThanSeven.isPresent()) {
        System.out.println("The number: " + numbersGreaterThanSeven.get());
    } else {
        System.out.println("Number is not available!");
    }

    //isEmpty version
    if (numbersGreaterThanSeven.isEmpty()) {
        System.out.println("Number is not available!");
    } else {
        System.out.println("The number: " + numbersGreaterThanSeven.get());
    }
}

Output

isEmpty() in java 11

GitHub Project

Java 11 Features with Examples

In this article, I have shared the Java 11 new features for String operations, Files API, and Optional isEmpty() method. You can see the examples on the swtestacademy GitHub page.

Thanks for reading,
Onur Baskirt

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.