Java_Magazine_NovemberDecember_2018

(singke) #1

58


//java future/


of primitive ints. Because these values are primitive types and not objects, they are laid out at
adjacent memory locations.
To see the difference with object arrays, contrast this with the boxed integer case. An array
of Integer objects will be an array of references, as shown in Figure 2, with each Integer having
to pay the “header tax” that comes with being a Java object.

For more than 20 years, the current memory layout has been the way the Java platform
has worked. It is relatively simple, but there is a performance trade-off: Working with arrays of
objects involves unavoidable pointer indirections and the cost of cache misses.
As an example, consider a class that represents a point in three-dimensional space, a
Point3D type. It really comprises only three primitive doubles (for the three spatial coordinates)
and, as of Java 11, it is represented as a simple object type that has three fields:

public final class Point3D {
private final double x;
private final double y;
private final double z;

public Point3D(double a, double b, double c) {
x = a;

Figure 2. Array of Integer objects


Integer[] M K 3


M K 14


M K 25


M K 6

Free download pdf