Java Reverse Engineering Tutorial#1: Study of Boolean Datatype Size

Aim:

Whats the size of Boolean Datatype in Java ? 1bit, 8bit, etc ?
In this tutorial we will study the size of the most speculated datatype in Java: Boolean. The reason for this is that many people think that Boolean's size is 1 bit but thats not possible, so may be its the minimum possible 8bits. No. Its not either. Well, we will use reverse engineering to see actually what is the size actually assigned in the class file.

Tools Needed:


    Steps:

    1. Copy paste the following code in your favourite java sourcecode editor:
    class BooleanSize
    {
    public static void main(String args[])
    {
    boolean myboolean;

    myboolean=true;
    }
    }

    2. Save and Compile the Java Sourcecode file to generate class file.
    C:\Program Files (x86)\Java\jdk1.6.0\bin>javac BooleanSize.java

    3. A file named as "BooleanSize.class" is generated.

    4. Open this file in jd-gui java decompiler. You will see the following output:
    class BooleanSize
    {
    public static void main(String[] paramArrayOfString)
    {
    int i = 1;
    }
    }

    Explanation:

    So after decompiling the class file we observe that, the boolean variable that we declared is actually an 'int'. Why ? Because, the actual size of a boolean type is not defined correctly by the Java Specification itself.
    In the boolean section, it says: 
    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.
    So why 'int' ? Actually, it is VM-dependent. I used a 32bit Java Compiler to compile and it assigns size of an  'int' to a 'boolean'.

    Conclusion:

    Hence, the size of boolean datatype in Java is VM-dependent and not a fixed size like 1bit or 1 byte.