The Java reference here indicates that boolean types, while represented with a "bit" of information, do not have a precisely defined size. In contrast, other types seem to suggest that the size is defined. For example, an int is 32-bits, end of story.
When we look at the spec for a BitSet, we can see that it is composed of boolean values. By the reference above, this seems to suggest that the "size" of a BitSet is undefined - it's composed of boolean values, after all. And sure enough, the documentation specifies:
Note that the size is related to the implementation of a bit set, so it may change with implementation.
So my question is, why not implement a BitSet using another datatype that is precisely defined? For example, if we use a byte, we could guarantee a size of 8-bits, and we wouldn't have the fuzzy feeling that the size may not be what we think it is. It's true that the size would have to be divisible by 8, but at least it seems more size-deterministic this way.
If we have a system that absolutely cannot exceed a certain memory capacity, it seems useful to have a BitSet implementation that is precise in terms of size.
I think that you're getting conceptually stuck by the fact that the method signatures use booleans.
The easiest way to think about a single bit is off/on, so a boolean true/false is a convenient way to model it. Another thing entirely is the BitSet
internal storage, which if you have a look at the source code, is using a long
array and using bitmasks to twiddle individual bits.
Accordingly, the size of the BitSet
is tied pretty closely to the number of bits in use.