Skip to main content
Java 11 (LTS)
  1. Posts/

Java 11 (LTS)

·541 words·3 mins·
Roman
Author
Roman
Photographer with MSci in Computer Science and a Home Lab obsession
Table of Contents

This covers most of the major language changes from Java 8 (the last LTS) through Java 11.

String Methods
#

java.lang.String

New methods added to String in Java 11:

Return Type Method Description
boolean isBlank() true if empty or contains only whitespace
Stream<String> lines() Splits into a stream of lines by line terminators
String strip() Removes leading and trailing whitespace (Unicode-aware)
String stripLeading() Removes leading whitespace
String stripTrailing() Removes trailing whitespace
String repeat(int count) Repeats the string count times
"  hello  ".strip();         // "hello"
"  hello  ".stripLeading();  // "hello  "
"  hello  ".stripTrailing(); // "  hello"
"  ".isBlank();              // true
"ha".repeat(3);              // "hahaha"
// lines() - splits by \n, \r, or \r\n
"one\ntwo\nthree"
        .lines()
        .collect(Collectors.toList());
// [one, two, three]

trim() vs strip() trim - Was intriduced before Unicode was finished strip - is Unicode aware


var in Lambda Parameters
#

JEP 323

Java 10 introduced var for local variables, the type is inferred from the right-hand side:

var name = "Alice";           // inferred as String
var numbers = List.of(1, 2, 3); // inferred as List<Integer>

Java 11 extends this to lambda parameters for consistency. These three are all equivalent:

Function<String, String> f1 = (String s) -> s.toUpperCase(); 
// explicit type
Function<String, String> f2 = s -> s.toUpperCase();          
// inferred - no type at all
Function<String, String> f3 = (var s) -> s.toUpperCase();    
// inferred - var makes it explicit

var adds no new power on its own. Its only practical benefit is that it lets you add annotations to lambda parameters without spelling out the full type:

// Can't annotate a bare parameter - compile error
(@NonNull s) -> s.toUpperCase()

// Must use either full type or var
(@NonNull String s) -> s.toUpperCase()  // verbose
(@NonNull var s) -> s.toUpperCase()     // var keeps it concise

var must be used for all parameters or none: (var x, y) is a compile error.


HTTP Client
#

JEP 321 - java.net.http

Launch Single-File Programs
#

JEP 330

When it comes to writing a small program that fits into one file program before java 11 it would need to be compiled first like so:

$ javac SimpleApp.java
$ java SimpleApp.java
Hello from simple App

Instead now Java 11 can compile it directly with one command

$ java SimpleApp.java
Hello from simple App

Run a single .java file directly without compiling first.

java Hello.java

Factory Methods for Immutable Collections
#

JEP 269 - Introduced in Java 9, these static factory methods create compact, truly immutable collections.

List
#

List.of()

List<String> list = List.of("a", "b", "c");

Set
#

Set.of()

Set<String> set = Set.of("a", "b", "c");

Duplicate elements throw IllegalArgumentException at construction time.

Map
#

Map.of() - for up to 10 key-value pairs:

Map<String, Integer> map = Map.of(
    "one", 1,
    "two", 2,
    "three", 3
);

Map.ofEntries() with Map.entry() - for more than 10 entries:

Map<String, Integer> map = Map.ofEntries(
    Map.entry("one", 1),
    Map.entry("two", 2),
    Map.entry("three", 3)
);

Map.entry() creates a single immutable Map.Entry and can also be used standalone.
#

Sources
#