text: Java array of Object that does not require new: allocated contiguosly - Stack Overflow
  Java array of Object that does not require new: allocated contiguosly
  In Java you declare an array and then you call new to allocate space. so that a class with 4 named integers takes less space and has better locality than an array of size 4.
  Is there any way to have an array of 4 elements, but have it allocated the same way as named vaiables a1, a2, a3, a4
  For those that know C++, this is the same as asking for int x[4] as opposed to int *x
  [ Class OID ][x1][x2][x3][x4] = 1 ref + 4 int
  y=new int[4];
  [ Class OID ][Y] ======> [Array OID][Array Size][y][y][y][y] = 3 ref + size + 4 int
  java arrays optimization data-structures share|improve this question edited Nov 15 '13 at 19:57
  asked Nov 15 '13 at 2:53
  A class with 4 named integers takes the same space as an array of 3 elements. The 4th element space is the array size (though sometimes this is absorbed into the header, and in other cases it's swallowed by allocation rounding). The allocation of an array is essentially the same as the allocation of an object with fields.
  @HotLicks - I thought it might be expensive - the pointer in the class is (on 64bit systems) the size of 2 integers - the runtime info for the array object is also a pointer - another 2 - plus the size - another 2 - and then the 4 integers = 10 ints instead of 4 for the same size plus the load which would add the possibility of a cache miss
  A Java int is 4 bytes, whether in an array or in a regular object. Both array and regular object must contain the class pointer. Now, if you're talking of embedding one object (whether array or regular object) in another, instead of using a pointer ("reference") to refer to the object, that can't be done. If you want to do that use C# (though I'm not sure that C# can really do it vs simply simulate it).
  @HotLicks - see pic above in Q - note how Y is a reference - which is good because you can change what it refers to, but it is bad because it adds a lot of space and causes 2 fetches
  Yes, as I said you cannot embed one object in another in Java.
  Starting with Java 6, under ideal circumstances, objects are actually allocated on the stack rather than on the heap -- in other words, just what you're asking for. In general, this is attempted for objects whose references don't leave the scope in which they are created. So the truth is, in fact, that sometimes "new" is absolutely free! Although it may seem that Java code is verbose and inefficient, there are several layers of optimizing compilers that turn what you write into surprisingly efficient machine code.
  share|improve this answer answered Nov 15 '13 at 3:42
  Allocation on the stack might be a nice improvement, but you still have two allocations instead of 1
  note that switch in bytecode is optimized, it does not test i sequentially but goes directly to the required code
  3: L4
  share|improve this answer edited Nov 15 '13 at 4:52
  74.4k883149
  Not the answer you're looking for?	Browse other questions tagged java arrays optimization data-structures or ask your own question.	asked