1

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?

Community
  • 1
  • 1
GuruKulki
  • 25,776
  • 50
  • 140
  • 201
  • Been answered many times, here's the first result from searching for "boolean size": http://stackoverflow.com/questions/1907318/java-boolean-primitive-type-size – kdgregory Jan 07 '10 at 19:11
  • Few days ago ( less than week ) there was same topic. I recommend you to use searching – Gaim Jan 07 '10 at 19:13

6 Answers6

4

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

Asaph
  • 159,146
  • 25
  • 197
  • 199
3

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

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • Does that mean that `int`s are as fast and memory efficient as `boolean`s in Java when not used in an array? – Jori Jul 17 '13 at 11:23
2

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

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

Jeff
  • 21,744
  • 6
  • 51
  • 55
0

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.

Drewen
  • 2,806
  • 1
  • 15
  • 12
0

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

Abel
  • 56,041
  • 24
  • 146
  • 247