Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the size of Boolean array in Java

Tags:

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

like image 727
Hilmi Avatar asked Jun 26 '12 07:06

Hilmi


1 Answers

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.

like image 171
Tomasz Nurkiewicz Avatar answered Sep 26 '22 02:09

Tomasz Nurkiewicz