Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the size of a boolean variable in Java?

Tags:

java

boolean

Can any one tell the bit size of boolean in Java?

like image 605
DonX Avatar asked Dec 20 '08 18:12

DonX


People also ask

What is boolean variable size?

Boolean variables are stored as 16-bit (2-byte) numbers, but they can only be True or False.

What is a boolean variable in Java?

Answer: Boolean is a primitive data type in Java that has two return values. A boolean variable can return either “true” or “false”.

Why is a boolean 8 bytes?

A boolean type normally follows the smallest unit of addressable memory of the target machine (i.e. usually the 8bits byte). Access to memory is always in "chunks" (multiple of words, this is for efficiency at the hardware level, bus transactions): a boolean bit cannot be addressed "alone" in most CPU systems.

Why size of boolean is not defined in Java?

The Java Virtual Machine Specification says that there is limited support for boolean primitive types. There are no Java virtual machine instructions solely dedicated to operations on boolean values.


1 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.

like image 133
Jon Skeet Avatar answered Oct 04 '22 23:10

Jon Skeet