Java Reference
In-Depth Information
There are also value types , which are immutable and which don't have reference identity;
primitive types are a subset of this wider notion. We could then allow user-defined value types
(perhaps starting with a lowercase letter to emphasize their similarity to primitive types such as
int and boolean). On value types, == would, by default, perform an element-by-element
comparison in the same way that hardware comparison on int performs a bit-by-bit comparison.
You can see this being overridden for floating-point comparison, which performs a somewhat
more sophisticated operation. The type Complex would be a perfect example of a non-primitive
value type; such types resemble C# structs .
In addition, value types can reduce storage requirements because they don't have reference
identity. Figure 16.1 illustrates an array of size three, whose elements 0, 1, and 2 are light gray,
white, and dark gray, respectively. The left diagram shows a typical storage requirement when
Pair and Complex are Objects and the right shows the better layout when Pair and Complex are
value types (note that we called them pair and complex in lowercase in the diagram to
emphasize their similarity to primitive types). Note also that value types are also likely to give
better performance, not only for data access (multiple levels of pointer indirection replaced with
a single indexed-addressing instruction) but also for hardware cache utilization (due to data
contiguity).
 
Search WWH ::




Custom Search