06.09.2021 Views

Java with BlueJ, 2016a

Java with BlueJ, 2016a

Java with BlueJ, 2016a

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

36 CHAPTER 2. BASICS<br />

2.3.4 boolean Data Type<br />

The boolean type has two values: true and false. We will see that the<br />

boolean type can be useful when we discuss control structures in the next<br />

chapter. There are three operators defined for booleans: and, or and not<br />

represented in <strong>Java</strong> as &&, ||, and! respectively. && and || are binary<br />

operators where the operator appears between the two operands; ! is a<br />

unary operator that precedes its operand.<br />

Boolean operators<br />

operator in <strong>Java</strong> meaning<br />

AND && Evaluates to true if and only if both operands are true;<br />

evaluates to false otherwise.<br />

OR || Evaluates to true if at least one operand is true;<br />

evaluates to false if both operands are false.<br />

NOT ! NOT: negates the operand.<br />

Three truth tables below show the results for Boolean operators for all possible<br />

values of their operands.<br />

Boolean operation of AND<br />

a b a && b<br />

true true true<br />

false true false<br />

true false false<br />

false false false<br />

Boolean operation of OR<br />

a b a || b<br />

true true true<br />

false true true<br />

true false true<br />

false false false<br />

Boolean operation of NOT<br />

a<br />

! a<br />

true false<br />

false true<br />

The following table gives some example boolean expressions. The last 3<br />

examples are complex expressions. To understand those evaluations we need

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!