5 > 2 || 12 <= 7 T 5 > 2 && 12 <= 7 F 3 == 8 || 6 != 6 F 3 == 8 && 6 != 6 F
x | !x |
---|---|
true | false |
false | true |
The NOT operator in Java is this: ! — exclaimation point. The NOT operator changes true to false and false to true, as seen in the table. This may seem like a silly thing to do, but often it is useful. Sometimes it is more natural to express a condition in a particular way, but the program logic calls for the reverse of what you have written. Time for the NOT operator.
Say that you are shopping for a new computer. You want more than 800 MHz and more than 128 Meg of RAM. Here is s program fragment:
if ( ______(speed > 800 && memory > 128) ) System.out.println("Reject this computer"); else System.out.println("Acceptable computer");
As it is written, the program is wrong: a computer that exceeds your requirements will be rejected.