Tutorial :What is the bit depth of a boolean in java? [duplicate]


Possible Duplicate:
Java - boolean primitive type - size

Possible duplicate: Why is Java's boolean primitive size not defined?

Someone asked me a question, but I didnt get the question properly. is it related to size? If yes what's the answer?


From Sun's Java Tutorials:

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.

-- http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html


The answer is "JVM specific"

A boolean represents one bit of information.
How it is stored (whether one boolean per memory word, or packed with other boolean values in a single word, or in some radically different way that takes up 1000 bytes per boolean) is up to the JVM, as long as the other JLS provisions for primitive types are met.

It is probable the expression "bit depth" refer to the "number of bits per XXX" (here "number of bit(s) per boolean), as it does for pictures (x bits per data pixels).

alt text

Each pixel of an 8 bit image is describe in one of a possible 256 tones or colors.
Each pixel of a 24 bit image is describe in one of a possible 16.7 million tones or colors.
The higher the 'bit depth' of the image the greater the color or tonal quality.


Java Byte Code does not have a datatype 'boolean', therefore boolean primitives are converted to integer (32 bit) (except boolean arrays, where a boolean value is represented by a bit).


I guess the actual size in memory depends on the JVM, but in theory a boolean can be represented as a 1 or 0 (1 bit).


A boolean is a true or false value, and can be stored in a lonely, single bit of memory. But the exact requirements are not outlined in the spec, and some operating systems cannot write to a bit, but minimally, to a byte, so a byte might end up being used by default, even thout only a bit is required.


"The bit depth" may relate to how much values a boolean can take. In most languages, it can take two values: true or false. In object oriented languages, if a boolean is an object (like Boolean in Java), the value can also be null (and some languages, apart from null, support a state called uninitialized, which can mean any value).

The size of the boolean in memory is an entirely different question. As already mentioned, for Java it is undefined, and if I recall correctly, it will likely depend on the WORD size of the system the program runs on. Theoretically, a boolean can be one bit in size, but only when we need many booleans, we can use that (called flags variables), because we are bound to the WORD boundaries of any system.

The size in the compiled image can be different again (in the event of constants), but you normally shouldn't need to worry about that.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »