Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Actual size in bytes occupied by an array of x datatype in java?

How can "size in bytes" of a array of some datatype be determined?

What makes me ask this is a question from this icse board question paper.

The question is as follows:

And the accepted answers by the board are (space occupied by one of those data types*no of elements in the array)

For example , one int array of 10 objects is said to take 4*10=40Bytes

But my question is, wouldn't the array have to store it's length and indexes somehow(increasing it's size on memory), or am I misunderstanding the question?

like image 956
Amith KK Avatar asked Feb 17 '14 15:02

Amith KK


1 Answers

Your understanding is indeed correct. The official answer adopts a simplistic view of how arrays are stored.

There is a fixed cost associated with every object. Additionally, arrays need to keep track of their size and possibly other things.

The answer also ignores issues such as padding, alignment, etc.

like image 123
NPE Avatar answered Oct 30 '22 03:10

NPE