Java Reference
In-Depth Information
result=a<b||c!=0; //Case 4 - result is false
In case 1, the result evaluates to true because both relational elements in
the statement are true. While case 4 evaluates to false because the OR con-
nector requires that at least one of the relational elements be true and, in
this case, both are false ( a > b and c = 0).
The logical NOT operator is used to invert the value of a boolean variable
or to test for the opposite. For example:
boolean result;
boolean tf = false;
result = (tf == !true); // result is true
The preceding statement evaluates to true since !true is false and tf is
false. The principal use of conditional expressions is in making program de-
cisions, the topic of Chapter 9.
Manipulating bits
Youalreadyknowthatcomputersstoredatainindividualelectroniccellsthat
are in one of two states, sometimes callled ON and OFF. Also, that these two
states are represented by the binary digits 1 and 0. In practical programming
youoftendisregardthisfact,andwritecodethatdealswithnumbers,charac-
ters,booleanvalues,andstrings.Storinganumberinavariableoftypedouble,
or a name in a String object, does not usually require dealing with individual
bits.However,therearetimeswhenthecodeneedstoknowthestateofoneor
more data bits, or needs to change individual bits or groups of bits.
One of the reasons for manipulating individual bits or bit fields is simple
economics. Suppose that you were writing an operating system program and
needed to keep track of the input and output devices present in the machine.
For example, your code may need to determine and store the following in-
formation:
The number of printers (range 0 to 3).
If there is a mouse installed (yes or no).
The number of serial ports (range 0 to 7).
If the Pentium CPU is equipped with MMX technology (yes or no).
One way to store this information would be in conventional variables.
You could declare the following variable types:
int printers;
boolean mousePresent;
int serialPorts;
boolean hasMMX;
Search WWH ::




Custom Search