Some classes, such as java.util.Optional and java.time.LocalDateTime, are value-based. Such Instances of a value-based class are final and immutable. Such classes have annotation @jdk.internal.ValueBased and Java 16 now generates compile time warnings in case such classes are synchronized using synchronized keyword. Wrapper classes are value based. For example, Double class is a value based.
Example
package java.lang;
@jdk.internal.ValueBased
public final class Double extends Number
implements Comparable<Double>, Constable, ConstantDesc {
//...
}
Consider the following example:
Java16Tester.java
Example
public class Java16Tester {
public static void main(String[] args) {
Double d = 10.0;
synchronized (d) {
System.out.println(d);
}
}
}
Compile and Run the program
$javac Java16Tester.java
Output
Java16Tester.java:4: warning: [synchronization] attempt to synchronize on an instance of a value-based class
synchronized (d) {
^
1 warning