Gaurav S
alvi

New Features Of Java (post version 8)

This cheatsheet is to list new features which i use it generally during programming. Note - Only standard features are listed and preview ones are ignored.

Java 9

  1. New factory methods in collections: List.of(), Map.of(), Set.of().

  2. Stream has new iterate and takeWhile method.

  3. Optionals - ifPresentOrElse method.

  4. Interfaces can have private methods.

Java 10

  1. var keyword - local variable type inference

Java 11

  1. String has new methods : isBlank(), .lines(), .strip()

  2. var can be used in lambdas also along with its previous support in method scopes.

Java 14

  1. Switch Expressions

  2. Helpful NullPointerException - Exception now also shows variable which was null

Java 15

  1. Text Blocks

Allows defining multiline string literal.

public class TextBlockDemo {

    private static String textBlock = """ 
            Hello World !
            I am a multi-line text block and  
            you can start using me starting Java 15
            """;

    public static void main(String[] args) {
        String localTextBlock = """
                I am another text block
                but as a local variable
                """;
        System.out.println(textBlock);
        System.out.println(localTextBlock);
    }
}

Helpful to define String variables like JSON, HTML, CSV, etc.

Hope you find this useful.

For any comments feel free to contact on: grsalvi@gmail.com
Share