© 2021 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.
:troll:
Allow the use of C++14 language features in JDK C++ source code
Build-time changes needed.
equals()
), not identity(==
)Examples
java.lang.{Byte, Short, Integer,
Long, Float, Double, Boolean, Character}
java.util.{Optional, ...}
java.time.{Instant, ZonedDateTime, Duration,...}
List.of(), List.copyOf(), Set.of(), ..., Map.entry()
jdk.internal.@ValueBased
Because of their nature, value objects should not be:
==
synchronized(object)
This JEP is to make developers more aware of that and raise compiler and runtime errors when possible, to prepare for project Valhalla going GA.
synchronization
warnings emitted by javac when selected -Xlint:synchronization
synchronized(valueBasedObject)
-XX:DiagnoseSyncOnValueBasedClasses=1
- fatal-XX:DiagnoseSyncOnValueBasedClasses=2
- logging to console and
JFR
jdeprscan
for 3rd party bytecode
sealed interface LibraryType
permits AbstractLibTypeImpl, NoOpLibTypeImpl {}
sealed abstract class AbstractLibTypeImpl
implements LibraryType
permits LibTypeImpl1, LibTypeImpl2 {}
final class LibTypeImpl1 extends AbstractLibTypeImpl {}
final class LibTypeImpl2 extends AbstractLibTypeImpl {}
final class NoOpLibTypeImpl implements LibraryType {}
class UserLibTypeImpl
// this doesn't work
// extends AbstractLibTypeImpl implements LibraryType
{}
class SomeClass {
AbstractLibTypeImpl typeImpl;
void method(LibraryType libType) {}
}
Modules were introduced in Java 9 and brought warnings about illegal access.
Some suggested "these are just warnings, ignore them". Worse, some followed this "advice".
--illegal-access=permit
was promised to be temporary.
In Java 16 --illegal-access=deny
was default and deprecated.
setAccessible(true)
--illegal-access=debug|warn
and start adding --add-opens
--illegal-access=permit
or don't upgrade to Java 16 and keep producing legacy code for future generationsRoses are redIllegal access in Java 16 A.K.A. 'My program crashes!'
reflection may hit
you shall not use
--illegal-access=permit
--illegal-access=deny
--illegal-access=...
is no more!
if (object instanceof Integer) {
Integer integer = (Integer) object;
int doubled = integer * 2;
}
if (object instanceof Integer integer) {
int doubled = integer * 2;
}
public static boolean nonEmptyString(Object obj) {
return (obj instanceof String str) && !str.isEmpty();
}
instanceof
final
public static void whoAreYou(Object obj) {
if (obj instanceof Serializable serializable) {
System.out.println("You're serializable " + serializable + "!");
}
if (obj instanceof String string && string.length() >= 3) {
System.out.println("You're String, at least 3 characters long!");
} else if (obj instanceof Integer integer && integer > 0) {
System.out.println("You're a positive Integer");
} else if (obj instanceof Long aLong && (aLong < -9 || aLong > 9)) {
System.out.println("You're a Long with at least two digits");
}
}
Good old switch
remains the same
private static String getGreeting(Object version) {
return switch (version) {
case null -> throw new IllegalArgumentException("Impossible!");
case Integer i && (i >= 17) -> "Hello, this is Java™ "+ i + " ;-)";
case Integer i -> i +"? But how?";
default -> "oops...";
};
}
switch
can be now more null
friendlyswitch
handles now more than primitives, enum
and String
case
s can contain guardssealed
classes don't need default
switch
instruction works with PM toorecord
is a restricted form of classjava.lang.Record
record Complex(double real, double imaginary) {}
private final
fieldsequals
, hashCode
, toString
{}
native
methodsequals()
and hashCode()
!As a by-product, interface
s and enum
s can be declared as local too (apart from record
s).
Class.isRecord()
Class.getRecordComponents()
Annotations from components get "propagated" where their @Target(ElementType....)
permits to.
copy()
Records don't have any copy()
or with()
method.
That's why it's important for accessors, equals()
and hashCode()
to obey contracts!
instanceof
sealed
hierarchiesMove ZGC thread-stack processing from safepoints to a concurrent phase.
Remove all other per-thread root processing from ZGC safepoints.
Less than one millisecond should be spent inside ZGC safepoints on typical machines.
-XX:MetaspaceReclaimPolicy=(balanced|aggressive|none)
java.nio.ByteBuffer
sun.misc.Unsafe
Built around jdk.incubator.vector.Vector<E>
and its subclasses.
There are numerous domains where this explicitly vectorizing API may be applicable such as machine learning, linear algebra, cryptography, finance, and usages within the JDK itself.
The Vector API will benefit significantly from value types once ready.
New RandomGenerator
interface (and some new random algorithms), for cleaner approach and future extensibility.
Now all floating point operations are strictfp
.
Allow applications to configure context-specific and dynamically-selected deserialization filters via a JVM-wide filter factory that is invoked to select a filter for each individual deserialization operation.
java.net.UnixDomainSocketAddress
SocketAddress
to InetSocketAddress
will not always work nowProvides support for Unix domain sockets (AF_UNIX) in the java.nio.channels, SocketChannel, and ServerSocketChannel classes.
Stream.toList()
DateTimeFormatter.ofPattern("B")
Concurrently uncommit memory in G1
Support SHA-3 based signatures
Terminally deprecate ThreadGroup stop, destroy, isDestroyed, setDaemon and isDaemon