Java has no value types: objects allocated inplace, in contrast to objects referred by a pointer in the heap. This, in my opinion, has a negative impact on a program design and on a performance.
Incidentally, I've thought of a use case, which can be understood as a value type by the jvm implementations. Consider an example:
class A { private final B b = new B(); }
Implementation may layout class A, in a way that field b will be a content of an instance of class B itself rather than a pointer to an instance of a class B. This way we save a pointer and a heap allocation of instance B. Another example:
class C { C(int size) { values = new D[size]; for(int i = 0; i < values.length; i++) { values[i] = new D(); } } private final D[] values; }
Here field values is never a null and each item of array contains a non null value. Assuming these conditions are kept for a whole life cycle, and values are not passed by reference, we can consider values as an array of value types.
A use case conditions are following:
new
field = new T()
array[i] = new T()
Arrays.sort(array)
JIT's allowed to interpret a field as a value type provided it proves these conditions.
Later...
There is another use case to detect value types:
A variable can be layed out directly onto the stack, provided a preceding conditions are satisfied.
P.S. In spite that .NET has built in value types, it may use the very same technique to optimize reference types.
Remember Me
a@href@title, b, blockquote@cite, em, i, strike, strong, sub, super, u