Arithmetic, relational and logic operators (e.g. +, >, OR)

An operator in Java is a symbol that is used to perform operations, such as arithmetic, or comparisons, on values and variables. The values or variables that an operator acts on are called operands.

Arithmetic operators

Java has the usual arithmetic operators for addition (+), subtraction (-) and multiplication (*), which work as you would expect from ordinary arithmetic. It also has a division operator ( /) but particular care must be taken with division because the result depends on the data types of the operands. So, for example, if both operands are int types, the result is also an int. Hence, the Java expression 21/4 evaluates to 5 because the fractional part is discarded. If either (or both) of the operands of the / operator is a double (i.e. a number with a decimal point), the result is also a double. So, the mathematical expression 21.0/4 evaluates to 5.25.

The modulus operator (%) gives the integer remainder when two int values are divided. So, the Java expression 21 % 4 evaluates to 1.

The order in which the Java arithmetic operations are evaluated is the same as in ordinary arithmetic: *, / and % are evaluated first, in order from left to right and then + and -, again in order left to right. Parentheses may be used to change this order, in which case the operations in the parentheses are done first.

Relational operators

A relational operator such as less than (<), greater than or equal to (>=), equal to (==) or not equal to (!=) is used to compare two operands. Each of these operators returns a boolean value (either true or false).

Logic operators

A logic operator acts on boolean operands. The ! (NOT) operator flips a Boolean value, so true becomes false or false becomes true. The && (AND) operator returns true if both operands are true and false otherwise. The || (OR) operator returns true if either, or both, of the operands are true and false otherwise.

Often an expression involving logic operators also involves relational operators which need to be evaluated first.

Examples

The Java boolean expression:

(age < 18) || (age >= 65)

is read "age is less than 18 or age is greater than or equal to 65" and can be evaluated in steps as follows if age holds the value 65:

(65 < 18) || (65 >= 65)

false || true

true

The Java boolean expression:

!(age < 11) && (age != 100)

Is read "age is not less than 11 and age is not equal to 100".

If age is 11 the expression can be evaluated in steps as follows:

!(11 < 11) && (11 != 100)

!(false) && true

true && true

true

Java compared with Python

The Java operators +, - and * work the same way in Java as they do in Python. However, in Python the / operator always performs ordinary decimal division, resulting in a decimal number and there is a special symbol, //, for integer division.

The modulus operator in Java produces a negative result if its left operand is negative, whereas in Python it produces a negative result if its right operand is negative.

The comparison operators are the same in Java as they are in Python.

The Java logical operators, !, || and && work in the same way as their Python counterparts, but they are represented by different symbols in Python (not, or and and, respectively).

So, for example, in Python you might write this.

not A and B or C

whereas in Java you would write.

!A && B || C

Test your understanding

  1. Evaluate this Java expression when myInt contains the int value 9:

    myInt / 4 * 2 + 12

  2. Evaluate the following Java Boolean expression when count contains the int value 13:

    (count < 12) || !(count == 13)

  3. Write a Java expression that evaluates to true if myInt holds a value that is between 0 and 10 inclusive.