boolean

This is the data type used for true/false conditions. To speed up memory access, implementations don't pack boolean values into the theoretical one-bit minimum space, but put each boolean into a byte.

example declaration:

boolean b = false;

range of values: false, true

literals: false true

In the code below,

boolean found = false;

/* more code */

if (x == 99) found = true;

x is compared with value 99. If x matches 99, then the boolean found is set to true.

You cannot assign or cast a boolean value to any other type. However, you can always get the same effect by using an expression, like the following:

if (b) i=1; else i=0; // set i according to boolean b value.

// set boolean b according to int i value
if (i==1) b = true; else b = false;

Unlike some other languages, the Java boolean type is not based on integers. In particular, the Java programmer cannot increment, decrement, shift, or add boolean values. Inside a JVM, however, there are no instructions specifically for booleans, so integer operations (like assigning zero) are used. In the Sun JVM, the byte containing a boolean is promoted to 32 bits when pushed onto the stack during execution. So don't think booleans are a 1-bit type that economizes on storage.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.68.28