List<String> lineList = new ArrayList<String>();
// less duplication
var lines = new ArrayList<String>();
final var readLines = lines.size();
Merlin Bögershausen / @mboegie
A Project is a collaborative effort to produce a specific artifact, which may be a body of code, or documentation, or some other material.
The goal of Project Amber is to explore and incubate smaller, productivity-oriented Java language features
An enhancement is an effort to design and implement a nontrivial change to the JDK code base or to do some other kind of work whose goals, progress, and result are worth communicating broadly
Local-Variable Type Inference landed in Java 10
List<String> lineList = new ArrayList<String>();
// less duplication
var lines = new ArrayList<String>();
final var readLines = lines.size();
Local-Variable Syntax for Lambda Parameters landed in Java 11
BiConsumer<Consumer<String>, String> sink
= (x, y) -> x.accept(y);
// now with Annotations!
BiConsumer<Consumer<String>, String> safeSink
= (@Nonnull var x, var y) -> x.accept(y);
Multi-line Text Blocks landed in Java 11
String helloWorldResponse = """
<html>
<body>
<p>Hello, world</p>
</body>
</html>
""";
Switch Expressions landed in Java 14
int j = switch (day) {// enum Days {MONDAY, …}
case MONDAY, WEDNESDAY -> 0;
case TUESDAY -> 1;
default -> {
int k = day.toString().length();
yield k;
}
};
no more NullPointerExceptions
🥳
int j = switch (day) {// enum Days {MONDAY, …}
case null, MONDAY -> 0;
case TUESDAY -> 1;
default -> {
int k = day.toString().length();
yield k;
}
};
exhaustive, default
or compilation error 🥳
boolean goToWork = switch (day) {// enum Days {MONDAY, …}
case null, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY -> true;
case SATURDAY, SUNDAY -> false;
};
Records landed in Java 16
record Range(int low, int high) {
Range {
if (low > high)
throw new IllegalArgumentException(/*...*/);
}
int lenght() { return high - low; }
}
normal final class
⇒ usual overloading
record Range(List<Integer> values) {
Range(List<Integer> values) {
this.values = List.copyOf(values);
}
Range(Integer i1, Integer i2) {
this(List.of(i1,i2));
}
}
Sealed Classes landed in Java 17
sealed interface Shape {
sealed class Cornered implements Shape {}
final class Weirdo implements Shape {}
}
different compile Unit ⇒ permits
needed
sealed interface Cornered implements Shape
permits Rectangle, Square {}
final class Rectangle implements Cornered {}
final class Square() implements Cornered {}
sealed
have to be propagated
sealed interface Shape {
non-sealed class Circular implements Shape {}
sealed class Cornered implements Shape {}
final class Rectangle implements Cornered {}
record Square(int h) implements Cornered {}
}
landed in Java 16
if (o instanceof Triangle t) {
System.out.println("Triangle with area: "
+ t.calculateArea());
} else {
System.out.println("Not a triangle);
}
Mixing of patterns and operators possible
if (o instanceof Triangle t) {
System.out.println("Area: " + t.calculateArea());
} else if (!(o instanceof String str)) {
System.out.println("Not Triangle or String ");
} else {
System.out.println("String value is:" + str);
}
3rd Preview in Java 19
switch (s) {
case Triangle t -> System.out.println("Triangle");
case Square s -> System.out.println("Square");
default -> System.out.println("Something");
}
Guarded Patterns with when
switch (s) {
case Triangle t when t.calculateArea() > 100 ->
System.out.println("Large");
case Triangle t -> System.out.println("Small");
default -> System.out.println("Non-triangle");
}
1st Preview in Java 19
record Point(int x, int y) {}
void printSum(Object o) {
if (o instanceof Point(int x, var y)) {
System.out.println(x+y);
}
}
Lambda Leftovers no target, yet
BiFunction<Integer, String, String> f
= (i, _) -> String.valueOf(i);
class User {
private final String name;
Integer nameLength() = name::length;
}
Jim Laskey and Brian Goetz Sep. 2012
int x = 10, y = 20;
TemplatedString s = "\{x} + \{y} = \{x + y}";
// Concatenation: 10 + 20 = 30
Source Code: github.com/MBoegers/TreeExperiments
sealed interface Tree { Tree delete(int value); }
record SubTree(Tree left, int value, Tree right)
implements Tree {}
record EmptyTree() implements Tree {}
// SubTree#delete(int)
if (value == v) {
if (left instanceof EmptyTree
&& right instanceof EmptyTree) return new EmptyTree();
else if (left instanceof EmptyTree
&& right instanceof SubTree) return right;
else if (left instanceof SubTree
&& right instanceof EmptyTree) return left;
else if (left instanceof SubTree l
&& right instanceof SubTree)
return l.insertInRightTree(right);
else throw new IllegalStateException(
"This should not be possible");
} else //...
// TreeOperations#delete(int)
return switch (tree) {
case EmptyTree e, null -> e;
case SubTree(Tree l,Integer v,Tree r)
when contains(l, value) -> delete(l, value);
case SubTree(Tree l,Integer v,Tree r)
when contains(r, value) -> delete(r, value);
case SubTree(EmptyTree l,Integer v,EmptyTree r)
when (v == value) -> new EmptyTree();
case SubTree(SubTree l,Integer v,EmptyTree r)
when (v == value) -> l;
case SubTree(EmptyTree l,Integer v,SubTree r)
when (v == value) -> r;
case SubTree(SubTree l,Integer v,SubTree r)
when (v == value) -> insertSubTree(r, l);
case SubTree subTree -> subTree;
};
Dukes from OpenJDK: Duke
Amber stone image from unknown author under CC-BY
JUG Bonn Logo from Twitter
Binary Tree from Wikipedia
Other Images from adesso SE