Boolean data type only evaluates to true
or false
, so it is always going to take only one bit of memory. So why is there a need of extra 7 bit of memory, isn't it a waste of memory?
5 Answers
I think it may need more than 8 bits. It depends on JMV." In Oracle JVM primitive boolean
needs 8 bits, the reason is limited support and lack of optimization.
Read also: What is the size of a boolean variable in Java?
After The Java Tutorials - Primitive Data Types
boolean: The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that's precisely defined.
After The Java® Virtual Machine Specification
Although the Java Virtual Machine defines a boolean type, it only provides very limited support for it. There are no Java Virtual Machine instructions solely dedicated to operations on boolean values. Instead, expressions in the Java programming language that operate on boolean values are compiled to use values of the Java Virtual Machine int data type.
In Oracle’s Java Virtual Machine implementation, boolean arrays in the Java programming language are encoded as Java Virtual Machine byte arrays, using 8 bits per boolean element.
For example Boolean type looks in memory like this
header: 8 bytes
value: 1 byte
padding: 7 bytes
------------------
sum: 16 bytes
As an alternative to boolean[]
you can use for example java.util.BitSet.
Why is hard to store booleans as 1 bit? Read Vlad from Moscow answer. You cant address one bit of memory.
-
1
It depends of addressability of the memory. The least addressable unit is byte. You can take an address of a byte and do the address arithmetic with it. Moreover there are built-in machine commands that operate with bytes. However it is impossible to take the address of a bit and perform the address arithmetic. In any case at first you have to calculate the address of the byte that contains the target bit and apply additional machine commands that depend of the position of the bit in the byte that to set or reset this bit.
See Oracle’s Java Virtual Machine implementation:
Although the Java Virtual Machine defines a boolean type, it only provides very limited support for it. There are no Java Virtual Machine instructions solely dedicated to operations on boolean values. Instead, expressions in the Java programming language that operate on boolean values are compiled to use values of the Java Virtual Machine int data type.
The Java Virtual Machine encodes boolean array components using 1 to represent true and 0 to represent false. Where Java programming language boolean values are mapped by compilers to values of Java Virtual Machine type int, the compilers must use the same encoding.
You can see a discussion of the actual size used here:
What is the size of a boolean variable in Java?
But basically it comes down to a trade off between memory efficiency and performance - especially when you consider that every other variable in the class needs to start word-aligned anyway.
You can only gain by packing them smaller when you have multiple smaller objects to pack, and then you still need to potentially leave padding to align the next member.
isn't it a waste of memory?
Yes it is a waste of 7 bits, but as everyone of us know a byte is the smallest unit of storage, JVM says it to be of 1 byte.
-
4
-
1
-
-
4@MariuszS right, but you can't directly address bits. bytes are the smallest addressable unit of memory Nov 26, 2015 at 3:17
-
1
variable to value byte and bit number
. Also fetching a boolean value would require to perform AND operation (storing would require OR). So it was easier to implement booleans as full bytes (or int's)