what is the size of boolean data type in java?

Recommended Answers

All 7 Replies

It has two possible values: true and false.

What exactly do you want to know? eg:
If I add a single boolean variable to my class how much bigger is each instance I create?
or
If I write a single boolean to a file how big is the file?
or
If I write eight booleans to a file how big is the file?
or
What's the largest array of booleans I can create in a Megabyte of memory?
or what?

Java is a high level language remember. It's not C, you can't define record-like structures where you control the bit/byte placement of every item.
If you need to do some ultra-efficient boolean packing you can use shift/mask operators to pack single bits into ints that you can read/write as bytes to I/O streams

One byte of menory is requested for storage of a value/variable of boolean data type

One byte of menory is requested for storage of a value/variable of boolean data type

What's your source for this information? AFAIK there is no definition for this in the offical Java doc.

source for this information

JamesCherrill, thank you for correcting my answer. I am wrong. But, in Java what is the memory size of boolean data type in bits ?
I have just checked a Text Book "Java How to Program" by Deitel and Deitel. It is said:" The representation of a boolean is specific to the Java Virtual Machine on each computer platform." Can I say:"the memory size for boolean varies on different computer platform"?

James is right, this is taken from Suns Java tutorials on 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.

Can I say:"the memory size for boolean varies on different computer platform"?

Yes. Or, more precisely, "the memory size for boolean may varie on different computer platforms and different versions of the JVM"?

To answer the OP's question properly, we really need to know why he is asking.

Can I say:"the memory size for boolean varies on different computer platform"?

No, when someone asks you the size of a boolean, you state exactly what is stated in the JVM spec, no more, no less. This is because saying that "it varies on different platforms" would be more or less claiming that the change in platform would result in a change in the size of a boolean which isn't true.

Different VM implementations carry out optimizations which might result in a boolean occupying anywhere from 1 byte to 4 bytes (in case of a padding gone bad). I did read somewhere that some VM implementations translate a boolean to an int when writing out the class file. Not to mention that things get even more complicated when we consider 32/64 bit CPU with 32/64 bit OS combination. :-)

This is exactly the reason why people prefer using BitSet instead of an array of booleans; less overhead since each true/false bit is represented as a single bit always in BitSets.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.