© 2023 Piotr Przybył. Licensed under CC BY-NC-SA 4.0
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
Picture by Daniel Büscher from Pixabay
:troll:
compileJava {
options.compilerArgs += ["--enable-preview"]
}
test {
jvmArgs '--enable-preview'
}
run.jvmArgs += ['--enable-preview']
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<release>21</release>
<compilerArgs>
--enable-preview
</compilerArgs>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>--enable-preview</argLine>
</configuration>
</plugin>
Thread
s you currently have into Virtual Threads-Djdk.tracePinnedThreads=full
and JFR events
try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {
var namesSubtask = scope.fork(()-> nameService.getNames());
var scoresSubtask = scope.fork(()-> scoreService.getScores());
scope.join();
scope.throwIfFailed();
var names = namesSubtask.resultNow();
var scores = scoresSubtask.resultNow();
return combine(names, scores);
}
free cancellation within the three, yay!
StructuredTaskScope.ShutdownOnFailure()
StructuredTaskScope.ShutdownOnSuccess<T>()
static final ScopedValue<Level> SECURITY_CLEARANCE_LEVEL =
ScopedValue.newInstance();
// ...
ScopedValue.where(SECURITY_CLEARANCE_LEVEL, levelFromRequest())
.run(() -> {
// ...
var level = SECURITY_CLEARANCE_LEVEL.orElse(guestLevel());
if (level.permits()) {
doSomeStuff();
doSomeOtherStuff();
} else {
error();
}
});
Object o = ...
if (o instanceof Event e) {
if (e.field().value() > X) {
doSomething();
} else {
doSomethingElse();
}
}
Good old switch
remains the same
private static String getGreeting(Object version) {
return switch (version) {
case null -> throw new IllegalArgumentException("Impossible!");
case Integer i when (i >= 17) -> "Hello, this is Java™ "+ i + " ;-)";
case Integer i -> i +"? But how?";
default -> "oops...";
};
}
record Value(double value) {}
record Complex(Value real, Value imaginary) {}
// ...
switch (something) {
case Value(double v) ->
System.out.printf("I got the value %f%n", v);
case Complex(Value(double r), Value(double i)) ->
System.out.printf("I got the Complex [%f, %f]%n", r, i);
default ->
System.out.printf("Was ist das? %s%n", s);
}
Rekord(_)
: unnamed patternRekord(int _)
: unnamed pattern variablevar _ = Rekord(int a)
: unnamed variablefor
catch
var intArrayLayout = MemoryLayout.sequenceLayout(10, ValueLayout.JAVA_INT);
try (var arena = Arena.openConfined()) {
var segment = MemorySegment.allocateNative(intArrayLayout, arena.scope());
for (int i = 0; i < intArrayLayout.elementCount(); i++) {
segment.setAtIndex(ValueLayout.JAVA_INT, i, i);
System.out.print(segment.getAtIndex(ValueLayout.JAVA_INT, i));
}
}
private static short getUid() throws Throwable {
// calling https://man7.org/linux/man-pages/man2/geteuid.2.html
var nativeLinker = Linker.nativeLinker();
var stdlib = nativeLinker.defaultLookup();
var getuid = stdlib.find("getuid")
.orElseThrow(() -> new IllegalStateException("Cannot find `getuid`"));
var funcDesc = FunctionDescriptor.of(ValueLayout.JAVA_SHORT);
var getuidHandle = nativeLinker.downcallHandle(getuid, funcDesc);
return (short) getuidHandle.invoke();
}
java -XX:+UseZGC -XX:+ZGenerational -Xmx...
default behaviour is likely to flip in the future
get/add/remove
on First/Last
elementreversed()
collectionSequencedCollection
, SequencedSet
, SequencedMap
, Collections.unmodifiableSequencedCollection(seqColl)
"making " + strings + " with dynamic " + values() + "... does Java shine here?"
sb.append("Or maybe ")
.append(direct?we:you)
.append(" are just too ")
.append(grumpy()).append("?")
`... but ${myLanguage} can already do that!!!11one`
java.lang.StringTemplate.STR
imported to every source file like public static final
String intro = STR. """
Full name: \{firstName} \{middleName} \{lastName}
Born in: \{born.atZone(ZoneId.of("UTC")).getYear()}
(Exactly \{Duration.between(born, Instant.now()).getSeconds()} seconds ago)""";
fragments()
and values()
STR
process(proc)
turns string template into result using a StringTemplate.ProcessorBefore Java 21 (and still)
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Since Java 21
void main() {
System.out.println("Hello, World!");
}
String[] args
can be omittedmain()
doesn't have to be public
or static
String[] args
over none, static
over instance, public
over defaultjcmd [pid] JFR.view
jfr view