This covers most of the major language changes from Java 8 (the last LTS) through Java 11.
String Methods #
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()vsstrip()trim - Was intriduced before Unicode was finished strip - is Unicode aware
var in Lambda Parameters
#
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 explicitvar 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
varmust be used for all parameters or none:(var x, y)is a compile error.
HTTP Client #
Launch Single-File Programs #
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 AppInstead now Java 11 can compile it directly with one command
$ java SimpleApp.java
Hello from simple AppRun a single .java file directly without compiling first.
java Hello.javaFactory Methods for Immutable Collections #
JEP 269 - Introduced in Java 9, these static factory methods create compact, truly immutable collections.
List #
List<String> list = List.of("a", "b", "c");Set #
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 #
- openjdk.org: JDK 11
- JEP 269: Convenience Factory Methods for Collections
- JEP 321: HTTP Client (Standard)
- JEP 323: Local-Variable Syntax for Lambda Parameters
- JEP 330: Launch Single-File Source-Code Programs
- docs.oracle.com: java.lang.String (Java 11)
- docs.oracle.com: java.net.http (Java 11)
- DigitalOcean | Java 11 features
- Baeldung | Java 11 new Features