Here's what Nikolai Parlog writes about values-based classes on his blog:
In Java 8, value types are preceded by value-based classes. Their exact relation in the future is unclear, but it may be the same as with boxed and without boxed primitives (for example, Integer and int). In addition, the compiler can freely switch between them to improve performance. It is the switching back and forth, that is, the removal and subsequent re-creation of the link, that also prohibits applying mechanisms based on identity to classes based on values.
So Nikolay says this:
In the future, compilers may do things that transparently translate between values and value-based classes in ways that do not preserve the identity of the object.
Certain things ("identity-based mechanisms") depend on the identity of the object. Examples include semantics == for links, an identifier hash code, primitive lock and object serialization.
For these things, it is likely that a transparent translation will not be transparent.
In the case of primitive locking, the problem is this:
- An instance of the value-based class is created.
- The instance is converted to a value behind the scene.
- Then the value is converted back to give another object.
If the two threads then use the "instance" as a primitive lock, they may not know that there are actually two objects (now). If they then tried to synchronize , they would (could) block different objects. This would mean that there was no mutual exclusion from the state that the lock was supposed to protect.
Unless you block a value-based class, you don't have to worry about this potential danger ... in the future.
But note that posting on Nikolai’s blog is one assumption about what could happen in Java 10 or later.
By the way, I understand the reasons for not blocking Integer and other classes of primitive shells; they can be cached.
Caching is not a problem as such, but a mechanism that causes the problem. The real problem is that it is difficult to determine the identity of the object of the lock object and, therefore, whether the lock mode is reliable.
With primitive shells, it is the semantics of packaging and unpacking that creates the uncertainty of the identity of the object. Subsequently, the conversion of an object <-> of an object of type mooted will be another source of this uncertainty.
The above blog is based on The State of Values, April 2014. John Rose, Brian Goetz, and Guy Steele talk about adding value types to a future version of Java. This note is an expression of position, not a fully specified (and accepted) sentence. However, the note gives us this hint:
"Many of the above restrictions correspond to the restrictions for so-called value-based classes. In fact, it seems likely that the boxed form of each type of value will be a value-based class."
which can be construed as implying a relationship between value types and existing value-based classes. (Especially if you read between value lines of Java 8 value- based classes .)
UPDATE - 2019/05/18
Value types did not get into Java 12, and they are not (yet) in the list for Java 13.
However, it’s already possible to demonstrate the problem associated with the problem referred to in the blog post:
public class BrokenSync { private final Integer lock = 1; public void someMethod() { synchronized (lock) {
The problem is that each BrokenSync instance will create an Integer instance using BrokenSync 1 . But JLS says that Integer objects created by the autobox are not necessarily separate objects. This way you can get all BrokenSync instances using the same Integer object as a lock.