Hilmi
Hilmi

Reputation: 3441

What is the size of Boolean array in Java

As i know the boolean size in 16 bytes {8 as header,1 payload ,* alignment to 8}

how much does it take if the boolean variable was an array ...

my reference

Upvotes: 7

Views: 7660

Answers (2)

rshahriar
rshahriar

Reputation: 2470

The array Object size will be: 8 + 4 = 12 bytes (Here 4 is the length of array) If array length is N then the boolean elements will be: N*16 bytes So the size will be: (12 + N * 16) bytes rounded (ceil) by 8

As an example: if N =10, then 12 + 10 * 16 = 172 and after rounding the figure by JVM, the size will be 176 bytes.

Upvotes: -1

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 341003

Are you asking about Boolean object or boolean primitive? The size of the object might be 16 bytes (although probably implementation dependent) while boolean will probably consume 4 bytes (int is implicitly used).

Thus boolean[] will consume N * 4 bytes (where N is the size of the array) + some object header. Boolean[] will consume N * 16 + header (according to your assumption on Boolean size.

That being said consider writing your own array-like class and pack 32 booleans in one int (you'll have to write few bit operations manually), just like BitSet class is doing.

Upvotes: 7

Related Questions