3

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 X
{
   int x1;
   int x2;
   int x3;
   int x4;
};

[ Class OID ][x1][x2][x3][x4] = 1 ref + 4 int

class Y
{
   int y[];
};

y=new int[4];

[ Class OID ][Y] ======> [Array OID][Array Size][y][y][y][y] = 3 ref + size + 4 int
5
  • 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. Commented Nov 15, 2013 at 3:38
  • @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 Commented Nov 15, 2013 at 3:43
  • 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). Commented Nov 15, 2013 at 12:40
  • @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 Commented Nov 15, 2013 at 19:47
  • Yes, as I said you cannot embed one object in another in Java. Commented Nov 15, 2013 at 20:42

2 Answers 2

3

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.

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

1 Comment

Allocation on the stack might be a nice improvement, but you still have two allocations instead of 1
0

The only thing you can do is this

int x1, x2, x3, x4;

void set(int i, int v) {
    switch(i) {
    case 0:
        x1 = v;
        break;
    ...
    default:
        throw new IndexOutOfBoundsException();
    }
}

int get(int i) {
...

note that switch in bytecode is optimized, it does not test i sequentially but goes directly to the required code

ILOAD 1
TABLESWITCH
  0: L1
  1: L2
  2: L3
  3: L4
  default: L5

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.