Can any one tell the bit size of boolean in Java?
-
1The same is asked here: http://stackoverflow.com/questions/1907318/java-boolean-primitive-type-size/ – dma_k Mar 08 '10 at 11:39
8 Answers
It depends on the virtual machine, but it's easy to adapt the code from a similar question asking about bytes in Java:
class LotsOfBooleans
{
boolean a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, aa, ab, ac, ad, ae, af;
boolean b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf;
boolean c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, ca, cb, cc, cd, ce, cf;
boolean d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, da, db, dc, dd, de, df;
boolean e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, ea, eb, ec, ed, ee, ef;
}
class LotsOfInts
{
int a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, aa, ab, ac, ad, ae, af;
int b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf;
int c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, ca, cb, cc, cd, ce, cf;
int d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, da, db, dc, dd, de, df;
int e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, ea, eb, ec, ed, ee, ef;
}
public class Test
{
private static final int SIZE = 1000000;
public static void main(String[] args) throws Exception
{
LotsOfBooleans[] first = new LotsOfBooleans[SIZE];
LotsOfInts[] second = new LotsOfInts[SIZE];
System.gc();
long startMem = getMemory();
for (int i=0; i < SIZE; i++)
{
first[i] = new LotsOfBooleans();
}
System.gc();
long endMem = getMemory();
System.out.println ("Size for LotsOfBooleans: " + (endMem-startMem));
System.out.println ("Average size: " + ((endMem-startMem) / ((double)SIZE)));
System.gc();
startMem = getMemory();
for (int i=0; i < SIZE; i++)
{
second[i] = new LotsOfInts();
}
System.gc();
endMem = getMemory();
System.out.println ("Size for LotsOfInts: " + (endMem-startMem));
System.out.println ("Average size: " + ((endMem-startMem) / ((double)SIZE)));
// Make sure nothing gets collected
long total = 0;
for (int i=0; i < SIZE; i++)
{
total += (first[i].a0 ? 1 : 0) + second[i].a0;
}
System.out.println(total);
}
private static long getMemory()
{
Runtime runtime = Runtime.getRuntime();
return runtime.totalMemory() - runtime.freeMemory();
}
}
To reiterate, this is VM-dependent, but on my Windows laptop running Sun's JDK build 1.6.0_11 I got the following results:
Size for LotsOfBooleans: 87978576
Average size: 87.978576
Size for LotsOfInts: 328000000
Average size: 328.0
That suggests that booleans can basically be packed into a byte each by Sun's JVM.
-
2@warrior: As I'd got the code for "byte" already, changing it to "boolean" was pretty straightforward :) – Jon Skeet Dec 22 '08 at 07:56
-
3System.gc() doesn't guarantee cleaning memory. It just gives an order to the JVM to run the garbage collection, but it doesn't mean that the collector actually cleaned something. Remember the collector cleans up UNUSED objects. An object is unused if the program holds no more references to it. So in your test I would explicitly drop the reference by setting LotsOfBooleans to null before running gc(); OR just run main once with boolean, once with int then compare the numbers. – Randa Sbeity Mar 15 '14 at 18:16
-
2@RandaSbeity Or even better: make sure you preserve both references and calculate the memory difference. Which is exactly what happens here. – biziclop Feb 15 '15 at 22:42
-
For the unwary eye, those numbers in the output need to be divided by 80, because of there are 80 elements in the classes, to arrive at the final result asserted by the Answer. – flow2k Dec 09 '17 at 10:36
It's virtual machine dependent.

- 274,122
- 60
- 696
- 724

- 39,156
- 44
- 139
- 214
-
9Care to point to some docs? I find it hard to believe that the size of a boolean is machine dependant. That would mean that the binary representation of a class containing a boolean would have different sizes (and memory layouts) in different VM and that would imply that VMs would not be compatible. – David Rodríguez - dribeas Dec 20 '08 at 21:54
-
14I think it was implied that the question is referring to the size of a boolean variable in memory, not the size of a boolean variable encoded in a class file. The size in memory varies by VM according to Sun's documentation. The size in the class file is constant. – William Brendel Jan 06 '09 at 20:11
-
3@DavidRodríguez-dribeas - The Sun JVM around the time of Java 1.1 used 4 bytes for boolean when stored as an instance or auto var. This simplified the implementation of the bytecode interpreter (which regards bools as occupying 4 bytes on the stack) and was the path of least resistance. When we implemented the iSeries "Classic" JVM we found ways to make instance vars 1 byte, as that greatly improved the compactness of some objects (which has an amazing impact on performance). Apparently, based on posts below, the Sun/Oracle developers figured out how to do likewise in later versions. – Hot Licks Jul 02 '13 at 17:22
-
But it's right, as of late 2017 [JavaDocs](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html) say: `boolean: The boolean data type... This data type represents one bit of information, but its "size" isn't something that's precisely defined` -- but your point is valid, it could use some links and better info :) – JimLohse Dec 04 '17 at 21:26
The actual information represented by a boolean value in Java is one bit: 1 for true, 0 for false. However, the actual size of a boolean variable in memory is not precisely defined by the Java specification. See Primitive Data Types in Java.
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.

- 31,712
- 14
- 72
- 77
On a side note...
If you are thinking about using an array of Boolean objects, don't. Use a BitSet instead - it has some performance optimisations (and some nice extra methods, allowing you to get the next set/unset bit).

- 35,041
- 6
- 86
- 121
-
This is not always true http://stackoverflow.com/questions/605226/boolean-vs-bitset-which-is-more-efficient – Przemek Apr 05 '16 at 09:36
-
That answer suggests there are significant reasons to use boolean[], but as the comments there indicate, there isn't much to back it up. Having said that: I don't program much in Java (and didn't provide any evidence either ;) – Matthew Schinckel Apr 06 '16 at 06:31
I read that Java reserves one byte for a boolean
datatype, but it uses only one bit.
However, the documentation says that "its "size" isn't something that's precisely defined".
See here.

- 2,007
- 3
- 21
- 36
-
That's a tutorial,not 'the documentation'. The documentation is the JLS, JVM Spec., and the Javadoc. – user207421 Dec 10 '17 at 06:20
Size of the boolean in java is virtual machine dependent. but Any Java object is aligned to an 8 bytes granularity. A Boolean has 8 bytes of header, plus 1 byte of payload, for a total of 9 bytes of information. The JVM then rounds it up to the next multiple of 8. so the one instance of java.lang.Boolean takes up 16 bytes of memory.

- 481
- 1
- 3
- 12
-
I will tend to disagree, on HotSpot JVM 1.7.0_51 the header has 12 bytes + 1 for the boolean + 3 for the granularity. – Eugene Mar 20 '14 at 21:28
-
14
-
1Bytes or bits ? 16 Bytes for a `Boolean` wold be such a waste... that's the size of a `long` which can carry trillion times more information than a `Boolean` – Dici Feb 08 '17 at 10:06
-
2That is not necessarily how they are stored in memory, and I think that is what the person asking the question wanted to know. That document describes the class file format (compiled byte code), not the representation of a boolean variable in memory, because that is implementation-dependent. – William Brendel Dec 20 '08 at 18:25
It's undefined; doing things like Jon Skeet suggested will get you an approximation on a given platform, but the way to know precisely for a specific platform is to use a profiler.

- 63,018
- 25
- 139
- 189