3

Suppose there is an Integer array in my class:

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

}

On a 64 bit architecture the space requirement for this is ~ (20*8+24) + 24*20 {space required for references + some array overhead + space required for objects}.

Why java stores references to all of the 20 Integer objects? Wouldn't knowing that first memory location and the number of items in the array suffice? (assuming and I also as I read somewhere that objects in an array are placed contiguously anyways). I want to know the reason for this sort of implementation. Sorry if this is a noobish question.

5
  • What if you want to do Integer i = arr[3];? You want to reference the exact object, not the arr + 3, since the array might get GC'ed later. Commented Mar 21, 2013 at 14:44
  • In Java, everything has to be initialized before you can actually start using it. By default, all the elements of a reference type array are initialized to null. Commented Mar 21, 2013 at 14:48
  • That array is only holding space for references, initialized to null. You haven't created any Integer objects. Commented Mar 21, 2013 at 14:51
  • 1
    new Integer[20] just create an array reference(16 bytes) and 20 reference(4 bytes each) to null, Commented Mar 21, 2013 at 14:52
  • @GriffeyDog: Thanks for your reply. I meant "space requirement" in general sense. I will ofcourse fill the array in my program later. But, my question is intended more to references part i.e. 20*8, so 24*20 detail is insignificant to the question (although important). Commented Mar 21, 2013 at 15:00

1 Answer 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.