Boolean Expressions

Boolean variables or expressions can only have true or false values.

Testing Equality (==)

Primitive values like ints and reference values like Strings can be compared using the operators == and != (not equal) to return boolean values. The expression x == 4 evaluates to true if the memory location for variable x currently stores the value 4, otherwise the expression is false. Note that x == 4 does not assign a value to variable x, rather it simply compares the value of x to 4.

Note

The operator = changes the value of a variable. The operator == tests if a variable holds a certain value, without changing its value!

coding exercise Coding Exercise

What will the E01Equality program print out? Try to guess before you run it! Note that 1 equal sign (=) is used for assigning a value and 2 equal signs (==) for testing equality between values. The != operator tests for inequality.

Relational Operators (<, >)

The Relational Operators below in Java are used to compare numeric values or arithmetic expressions. Although some programming languages allow using relational operators like < to compare strings, Java only uses these operators for numbers, and uses the string methods compareTo() and equals() for comparing String values.

  • < Less Than

  • > Greater Than

  • <= Less than or equal to

  • >= Greater than or equal to

  • == Equals

  • != Does not equal

With <= and >=, remember to write the two symbols in the order that you would say them “less than” followed by “or equal to”.

coding exercise Coding Exercise

Try to guess what the E02RelationalOperators program will print out before you run it.

exercise Check your understanding

Testing with mod (%)

Here are some boolean expressions that are very useful in coding:

// Test if a number is positive
(number > 0)
//Test if a number is negative
(number < 0)
//Test if a number is even by seeing if the remainder is 0 when divided by 2
(number % 2 == 0)
//Test if a number is odd by seeing if there is a remainder when divided by 2
(number % 2 > 0)
//Test if a number is a multiple of x (or divisible by x with no remainder)
(number % x == 0)

Open the E03BooleanMod program. Try the expressions containing the % operator below to see how they can be used to check for even or odd numbers. All even numbers are divisible (with no remainder) by 2.

The modulo operator has a lot of great uses:

  • Use it to check for odd or even numbers (num % 2 == 1) is odd and (num % 2 == 0) is even. Actually, you can use it to check if any number is evenly divisible by another (num1 % num2 == 0)

  • Use it to get the last digit from an integer number (num % 10 = last digit on right).

  • Use it to get the number of minutes left when you convert to hours (num % 60).

  • Use it whenever you have limited storage and you need to wrap around to the front if the value goes over the limit (num % limit).

Negation !

You can use the ! operator to negate the value of a Boolean expression. When you see !, think of the word “not”.

Try to guess what the BooleanExpressions program will print out before you run it.

exercise Check your understanding

Summary

  • Primitive values and reference values can be compared using relational operators (i.e., == and !=) in Java.

  • Arithmetic expression values can be compared using relational operators (i.e., <, >, <=, >=) in Java.

  • An expression involving relational operators evaluates to a Boolean value of true or false.

You have attempted of activities on this page