my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes size of boolean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "size of boolean" Watch "size of boolean" New topic
Author

size of boolean

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Can somebody tell me what is the exact size of the primitive type boolean(in bits)? RHE says that it is 8 bits and Bruce Eckel's TIJ gives it as 1 bit.
Thanks.
bongadi
Greenhorn

Joined: Apr 17, 2000
Posts: 23
Check the errata of RHE.
I think it is in the errata.
Originally posted by udayan:
Can somebody tell me what is the exact size of the primitive type boolean(in bits)? RHE says that it is 8 bits and Bruce Eckel's TIJ gives it as 1 bit.
Thanks.

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

My copy of RHE says boolean uses one bit.
Regds.
- satya
Edward Man
Ranch Hand

Joined: May 16, 2000
Posts: 40
I compiled the following program:
<pre>
public class BoolTest {
public static void main (String [] args)
{
boolean b;
boolean f;
b = true;
f = false;
if (b != f)
System.out.println(b);
}
}
</pre>
I also disassemble it. The disassembled listing is below:
Method void main(java.lang.String[])
0 iconst_1
1 istore_1
2 iconst_0
3 istore_2
4 iload_1
5 iload_2
6 if_icmpeq 16
9 getstatic #6 <Field java.io.PrintStream out>
12 iload_1
13 invokevirtual #7 <Method void println(boolean)>
16 return

It appears to me that javac treats boolean as integer.
(iconst_x, istore_x, iload_x, if_icmpeq) are also integer op_codes.
So can someone confirm that boolean uses 1 bit, 8 bits, or 4 bytes?
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

Edward Man:
When you get a chance could you (or anyone else)
explain What "disassembling" means in the context
of java and how do I do it?
Some sort of debugging I guess...
Thanks.
- satya
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hey thanks for all the answers.Really appreciated it.
Thanks
Edward Man
Ranch Hand

Joined: May 16, 2000
Posts: 40
Satya,
The JDK comes with javap - the Java Disassembler.
try to type:
javap -help to get all the options.
I used the following command after I compiled BoolTest.java.
javap -c BoolTest > BoolTest.txt to get the disassembled output.
javap is used to display the class file in Java's assembler language. It helps to understand Java better; or you can use it for debugging as well.
It may be useful to look at Sun's JVM Specification if you want to know more.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

Thanks Edward.
- satya
bongadi
Greenhorn

Joined: Apr 17, 2000
Posts: 23
Hi!,
Thank you Edward for a great analysis.
However, I have tried answering the "size of a primitive"
question with the help of byte code but I could not go too far.
Check this out..
<pre>
class ByteCode {
public static void main (String av[]) {

boolean abool=true;
byteabyte=1;
shortashort=2;
char achar='a';
intanint=3;
/*
longalong=5L;
float afloat=4.0f;
doubleabouble=6.0;
*/
}
}
</pre>
Gets you the byte code (this is a relevant fragment)
<pre>
Method void main(java.lang.String[])
0 iconst_1
1 istore_1
2 iconst_1
3 istore_2
4 iconst_2
5 istore_3
6 bipush 97
8 istore 4
10 iconst_3
11 istore 5
13 return
</pre>
Here the primitives of type boolean, byte, short, integer
and even char are being stored as ints in the
local variable array of the frame.
IMHO it is better to stick with what the specs say. I would
say boolean is of size one bit.
Thoes who are interested in this kind of stuff there is a great
article at Java[tm] bytecode: Understanding bytecode makes you a better programmer
Enjoy
BonGadi

[This message has been edited by bongadi (edited May 14, 2000).]
[This message has been edited by bongadi (edited May 14, 2000).]
Edward Man
Ranch Hand

Joined: May 16, 2000
Posts: 40
The reason I want a confirmation of the size of a boolean is simple: I cannot find it in Sun's Java Language Specification.
I suspect Sun did not specify the size is most processors do not have machine instructions that recognise boolean as a data type. In Intel processors, manipulating an integer is more efficient.
If anyone can point me to the section of the JLS that mentions the size of boolean, I shall be grateful.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Check out this quote from the Java Virtual Machine Specification:
<blockquote>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 does directly support boolean arrays. Its newarray instruction enables creation of boolean arrays. Arrays of type boolean are accessed and modified using the byte array instructions baload and bastore.2
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.
</blockquote>
This is consistent with the fact that byte, short and char are almost always promoted to int the moment they are used in any calculations. Java is set up to assume that a four-byte int is the smallest natural word length on any machine it's running on, so in many cases there's little point in trying to use a smaller value.
And check out this footnote: <blockquote>In Sun's JDK releases 1.0 and 1.1, and the Java 2 SDK, Standard Edition, v1.2, boolean arrays in the Java programming language are encoded as Java virtual machine byte arrays, using 8 bits per boolean element.</blockquote>
And, from the API for writeBoolean() in DataOutputStream:
<blockquote>Writes a boolean to the underlying output stream as a 1-byte value. The value true is written out as the value (byte)1; the value false is written out as the value (byte)0.</blockquote>
Here, arrays and streams are set up to work in terms of bytes, not bits. They couldn't use 4-byte units here since things like ASCII text files and arrays of bytes are too common. Guess no one thought it was worthwhile to save space on an array of booleans though. (If you need to do that, I guess you'd need to instead encode the boolean array into an array of ints.)

"I'm not back." - Bill Harding, Twister
Edward Man
Ranch Hand

Joined: May 16, 2000
Posts: 40
Jim,
Thanks for your detailed explanation. It may be the reason why Bill Brogden's ExamCram does not mention the size of boolean. It is probably too complicated for the exam purpose.
I was too lazy not to read the whole JVM Specification (while studying for the exam).
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Well, I certainly haven't read the whole thing - but it's good to have a link to it on your list of useful Java references.
By the way Edward, if you register you'll be able to do cool stuff like editing or deleting your own posts after you've submitted them, which can be useful. Just ignore the fine print about how we'll own your soul afterwards.
Edward Man
Ranch Hand

Joined: May 16, 2000
Posts: 40
Thanks Jim,
I'll register after I come home. I won't simply go away.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
8 bits or 1 byte
This is exactly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: size of boolean
 
Similar Threads
boolean size
width of boolean
What is the size of boolean data type???
what is bit depth of a boolean ?
How much space does boolean takes up in Java ?