by gloomyandy » Wed Mar 28, 2012 11:49 am
Oh and do you mean Boolean or boolean? boolean is a primitive type, Boolean is an object that contains a boolean... Assuming you mean boolean then my previous answer is correct, if you mean Boolean then once you have read the following you can probably work it for yourself...
In which case you may need to understand a lot more than just the size of a boolean... Firstly an instance of a class has a fixed amount of overhead in the form of a header for a normal (none array class) this header is 4 bytes in size. Then there is is the issue of alignment, objects in memory are always stored aligned on 32 bit boundaries, fields within an object are packed and so are not aligned, though you will get slightly faster access to things like ints and longs if you can arrange the order of your fields such that they are correctly aligned (the linker generates faster byte code operations for correctly aligned fields). This object alignment means that an object contain a single boolean will not occupy 5 bytes (1 byte for the boolean plus the 4 byte header), but will actually occupy 8 bytes (with three unused padding bytes).
Since you plan to have an array of these objects, remember that in Java and array of objects is actually an array of references to an object so if say you have a class that has a single data field that is a boolean and you want to have 100 of these stored in an array then the actual memory used will be....
8 bytes for each object * 100 = 800 bytes
4 bytes per reference * 100 + array object header = 408 bytes
Andy