Programming Java

An Introduction to Computer Science



10. Boolean Expressions

Java code flows in sequential order until a control flow is encountered. Control flow gives a program decision-making ability. A Boolean expression creates the conditions for different decision paths. This chapter explains how to compose or evaluate a Boolean expression.

Boolean Type and Values

Primitive type boolean stores the two truth values of logic: true and false. These are called “Boolean values”. Boolean values represent any two opposing conditions such as Yes/No, On/Off, Found/Not Found, etc.

boolean isOn = true;	// turned on
isOn = false;			// turned off

boolean found = true;	// found
found = false;			// not found

Boolean Operators

Figure 10A – Boolean Operator Usage

Operand is a primitive value or an expression that evaluates to a primitive value.
Operand can also be a reference value or an expression that evaluates to a reference value.
Operator is one of the relational operators or one of the equality operators.


Terminology

Primitive value is the value assigned to primitive data types such as int, double, char, and boolean.

Reference value is the value assigned to reference data types which is an object data type. String is an object and is a reference data type.

Relational operators are Less Than (<), Greater Than (>), Less Than or Equal To (<=) and Greater Than or Equal to (>=).

Equality operators are Equals (==) and Not Equal (!=).

Java uses double equal symbol (==) as the equality operator because single equal symbol (=) is used as the assignment operator.

Be careful! It’s easy to confuse the equality operator with the assignment operator. The compiler will not catch the error.


Boolean operators perform comparisons of two operands and return a Boolean value.

1 < 2	// true
1 == 2	// false
1 != 2	// true

The operators (<=, >=) return true if either condition is met. Otherwise, they return false.

1 <= 1	// true
1 >= 2	// false

Boolean Expression

An expression that uses the relational operators or equality operators is called a Boolean expression. A Boolean expression always evaluates to a Boolean value.

1 < 2       // true
1 == 2      // false
1.0 >= 1    // true
'1' < 2     // false
"1" < 2     // compile-time error

Line 3 – The first operand is double type, and the second operand is int type. Integer 1 is converted to double 1.0 by the widening casting and then applied to the operator (>=).
Line 4 – The character '1' has an ASCII value of 49 in the computer system. Hence the expression ('1' < 2) evaluates to false.
Line 5 – The String literal "1" cannot convert to a numeric type. So, the comparison cannot happen. If the expression cannot evaluate to a Boolean value, a compile-time error occurs.

Consider the expression (1 < 2 < 3). Mathematically, this evaluates to true. Not in Java. The relational operators and the equality operators are left-associative in Java.

1 < 2 < 3
--> (1 < 2) < 3     // (1 < 2) is true
--> true < 3        // error
1 == 2 == 3
--> (1 == 2) == 3     // (1 == 2) is false
--> false == 3        // error

A Boolean value cannot convert to a numeric value. So, the comparison cannot happen. Hence the compile-time error.

The arithmetic expression is evaluated first and then applied to the Boolean operators.

1 + 1 >= 2	// true
x + y < 10	// true if (x + y) is less than 10
x % 2 == 0	// true if (x % 2) is 0

The equality operator checks for the exact equality.

0.3 == 0.1 + 0.1 + 0.1 	// false

Two operands of double type cannot be checked for the exact equality due to round-off error.

The Not Equal operator is useful when checking for an invalid condition before proceeding to the next step.

Calculate (x / y) on condition (y != 0) to avoid division by zero

2022 Copyright


By:


Design a site like this with WordPress.com
Get started