Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8339181

Java language implementation of value classes and objects

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • tools
    • None
    • source, behavioral
    • high
    • All instances of value classes, including classes like Integer, Optional, and LocalDate, behave differently under `==`, `!=`, and `synchronized`; existing `synchronized` statements may not compile
    • Java API, Language construct, add/remove/modify command line option
    • SE

      Summary

      Enhance the Java language with value classes and objects.

      Problem

      The JEP provides a high-level discussion motivating the feature.

      In --enable-preview mode, the Java language needs the following new capabilities:

      • A way to express, and validate, that a given class (abstract or concrete) has no dependency on identity
      • Constraints on instance creation of these classes, so that final fields can never be observed to mutate
      • Identity-free semantics for operations on reference types, including the == and != operators and the synchronized statement
      • Automatically interpreting certain JDK classes to have no identity at compile time

      Solution

      Our solution enhances the Java language as follows:

      • Introduce the value modifier for classes (not interfaces or enums, but including records); a value class is either abstract or implicitly final, and may not extend an identity class (other than Object); instance fields are implicitly final, and instance methods may not be synchronized
      • Require that the instance fields of a value class be assigned before the super() constructor call; where this call is implicit, place it at the end, not the start, of a value class constructor body
      • Specify when two value class instances are "the same", and so will successfully pass a == test; specify that a synchronized statement will throw when operating on a value class instance, and reject programs with synchronized statements operating on final value class types
      • Designate certain JDK classes as value classes when in --enable-preview mode

      Any class files that declare or use value classes are marked as preview-versioned class files. (This includes all classes that use, e.g., Integer or Optional.)

      javac also adds new checks to its -Xlint:serial warnings feature, identifying value classes that implement Serializable but that serialization cannot support (see also JDK-8317279).

      The javax.lang.model API and javadoc tool include trivial enhancements to surface the value modifier.

      A more comprehensive outline of the solution can be found in the JDK-8317277 description.

      Specification

      JLS changes are included in the attached spec change document bundle. See the file specs/value-objects-jls.html.

            dlsmith Dan Smith
            dlsmith Dan Smith
            Brian Goetz, Maurizio Cimadamore, Vicente Arturo Romero Zaldivar
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: