Java Preview
String Template
var name = "Duke";
var info = STR."My name is {name}";
System.out.println(info);
My name is Duke
Unamed Variables
try {
final List<String> strings = List.of("1");
strings.get(2);
} catch (Exception _) {
System.out.println("ups");
}
ups
Structured Concurrency API
Random r = new Random();
try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {
Supplier<Integer> number1 = scope.fork(r::nextInt);
Supplier<Integer> number2 = scope.fork(r::nextInt);
try {
scope.join().throwIfFailed();
System.out.println(number1.get());
System.out.println(number2.get());
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
}
}
-1656376899
1610822880
Scoped Values
Variable is valid with the value within the block and the it’s destroyed.
ScopedValue.where(NAME, "Ada").run(() ->
System.out.println(NAME.get()));
ScopedValue.where(NAME, "Aixa").run(() ->
System.out.println(NAME.get()));
private static final ScopedValue<String> NAME = ScopedValue.newInstance();
Ada
Aixa