Without getting in contact with people who were actually involved in these design decisions, I think we're unlikely to find a definitive answer. However, based on the timelines of the development of both Java and C++, I would conjecture that Java's boolean
was chosen before, or contemporaneously with, the introduction bool
to C++, and certainly before bool
was in wide use. It is possible that boolean
was chosen due to its longer history of use (as in Boolean Algebra), or to match other languages (such as Pascal) which already had a boolean
type.
Historical context
According to Evolving a language in and for the real world: C++ 1991-2006, the bool
type was introduced to C++ in 1993.
Java included boolean
in its first release in 1995 (Java Language Specification 1.0). The earliest language specification I can find is the Oak 0.2 specification (Oak was later renamed to Java). That Oak specification is marked "Copyright 1994", but the project itself was started in 1991, and apparently had a working demo by the summer of 1992.
In the old days of C, there was no boolean type. People used the int
for storing boolean data, and it worked mostly. Zero was false and everything else was true.
This meant if you took an int flag = 0;
and later did flag++
the value would be true. This would work no matter what the value of flag was (unless you did it a lot, it rolled over and you got back to zero, but lets ignore that) - incrementing the flag when its value was 1 would give 2, which was still true.
Some people used this for unconditionally setting a boolean value to true. I'm not sure it ever became idiomatic, but its in some code.
This never worked for --
, because if the value was anything other than 1 (which it could be), the value would still not be false. And if it was already false (0
) and you did a decrement operator on it, it wouldn't remain false.
When moving code from C to C++ in the early days, it was very important that C code included in C++ was still able to work. And so in the specification for C++ (section 5.2.6 (its on page 71)) it reads:
The value obtained by applying a postfix ++ is the value that the operand had before applying the operator. [Note: the value obtained is a copy of the original value ] The operand shall be a modifiable lvalue. The type of the operand shall be an arithmetic type or a pointer to a complete object type. After the result is noted, the value of the object is modified by adding 1 to it, unless the object is of type bool
, in which case it is set to true. [Note: this use is deprecated, see annex D. ]
The operand of postfix -- is decremented analogously to the postfix ++ operator, except that the operand shall not be of type bool
.
This is again mentioned in section 5.3.2 (for the prefix operator - 5.2.6 was on postfix)
As you can see, this is deprecated (Annex D in the document, page 709) and shouldn't be used.
But thats why. And sometimes you may see the code. But don't do it.
Best Answer
TL;DR The only thing that's sure is that
boolean
occupies at least one bit. Everything else depends on the JVM implementation.The Java Language Specification doesn't define sizes, only value ranges (see The Language Spec). So, it's not only the
boolean
size that's undefined at this level. Andboolean
has two possible values:false
andtrue
.The Virtual Machine Specification tells us that
boolean
variables are treated likeint
with values 0 and 1. Only arrays ofboolean
have specific support. So at the Virtual Machine level, aboolean
variable occupies the same amount of space as anint
, meaning one stack cell: at least 4 bytes, typically 4 bytes on 32-bit Java and 8 bytes on 64-bit.Finally there's the HotSpot engine that compiles JVM bytecode into optimized CPU-specific machine code, and I bet that in many cases it's able to deduce the limited value-range of an
int
-maskedboolean
from the context and use a smaller size.