22

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?

3
  • 2
    It actually takes more memory than just one 8bits - JVM usually implements booleans with int values. Why this is done that way? In order to understand that better, think about a JVM which has to handle several boolean values - if a boolean value were using only one bit, your JVM would need to keep mapping 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)
    – Tom
    Jan 6, 2014 at 11:04
  • Yes waste of memory for us, but efficient and less complex opcodes for Java people. They don't have to create specific opcodes for loading nd storing booleans from memory Jan 6, 2014 at 11:05
  • Question marked as duplicate has answer in question (lack of support), but the OP here want to know the reason. This are diffrent questions.
    – MariuszS
    Jan 6, 2014 at 12:24

5 Answers 5

14

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
  • 1
    i think this is the answer, thank u. I will research on it Jan 6, 2014 at 11:17
6

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.

3

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.

1

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.

0

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.

7
  • 4
    sorry, but not understood Jan 6, 2014 at 11:13
  • 1
    Byte is not the smallest unit, you can store bits in memory too. You can compact your data.
    – MariuszS
    Jan 6, 2014 at 11:39
  • Ok bits is the smallest unit. But aren't the 7 bits wasted in this case?
    – mtk
    Jan 6, 2014 at 14:12
  • 4
    @MariuszS right, but you can't directly address bits. bytes are the smallest addressable unit of memory
    – John Smith
    Nov 26, 2015 at 3:17
  • 1
    yes, but this is something else than smallest unit of storage ;)
    – MariuszS
    Jun 12, 2017 at 10:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.