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
Dukes from OpenJDK: Duke
Java Forum Stuttgart Favicon from JFS Conference Page
Amber stone image from unknown author under CC-BY
Other Images from adesso SE