3

假设我的班级中有一个整数数组:

public class Foo {
   private Integer[] arr = new Integer[20];
   .....

}

在 64 位架构上,对此的空间要求为 ~ (20*8+24) + 24*20 {引用所需的空间 + 一些数组开销 + 对象所需的空间}。

为什么 java 存储对所有 20 个 Integer 对象的引用?不知道第一个内存位置和数组中的项目数就足够了吗?(假设我也在某处读到数组中的对象无论如何都是连续放置的)。我想知道这种实现的原因。对不起,如果这是一个愚蠢的问题。

4

1 回答 1

2

Like every other class, Integer is a reference type. This means it can only be accessed indirectly, via a reference. You cannot store an instance of a reference type in a field, a local variable, a slot in a collection, etc. -- you always have to store a reference and allocate the object itself separately. There are a variety of reasons for this:

  • You need to be able to represent null.
  • You need to be able to replace it with another instance of a subtype (assuming subtypes are possible, i.e. the class is not final). For example, an Object[] may actually store instances of any number of different classes with wildly varying sizes.
  • You need to preserve sharing, e.g. after a[0] = a[1] = someObject; all three must refer to the same object. This is much more important (vital even) if the object is mutable, but even with immutable objects the difference can be observed via reference equality checks (==).
  • You need reference assignment to be atomic (cf. Java memory model), so copying the whole instance is even more expensive than it seems.

With these and many other constraints, always storing references is the only feasible implementation strategy (in general). In very specific circumstances, a JIT compiler may avoid allocating an object entirely and store its directly (e.g. on the stack), but this is an obscure implementation detail, and not widely applicable. I only mention this for completeness and because it's a wonderful illustration of the as-if rule.

于 2013-03-21T17:25:36.243 回答