> Hi everybody,
>
[quoted text clipped - 4 lines]
> Does a boolean[] waste space? And is this defined in some
> specification?
You may be thinking of the JVM specification. There are no special
bytecode instructions for manipulating booleans. The instructions for
reading/writing from/to boolean and byte arrays are the same. And in
early Sun VMs a boolean array did indeed use 8 bits per element. This
is however not mandated by the spec, a VM could very well use packed
arrays, but I /think/ most VMs will go with bytesized booleans. The
downside of packed arrays is that while you save some space, array
access becomes quite expensive. If you are worried about the extra
space use of boolean arrays, try using java.util.BitSet instead which
is almost guaranteed to use a packed int/long implementation.
It is not likely that a JVM would waste any space when allocating a
byte array, since bytes are readily accesible on just about any
processor, as opposed to single bits which require extra code to
access.
Regards,
Daniel Sjöblom
Mark Space - 07 Jul 2006 18:11 GMT
>> Hi everybody,
>>
[quoted text clipped - 9 lines]
> processor, as opposed to single bits which require extra code to
> access.
Well, any object in Java has about 8 bytes of overhead, at least. Do a
google search for "java memory object footprint," or similar, to get
some ideas. Primitives are NOT objects and don't have this issue. But
a single boolean could still take as much as 8 bytes, depending on the
JVM (esp. on 64 bit systems).
The best way to determine memory overhead is to get some of the test
programs that the google search above will give you, and try them out.