Topic 4: Operators

Arithmetic Operators

Arithmetic expressions in java can be evaluated using arithmetic operators such as addition (+), subtraction (-), multiplication (*), division (/), and modulus (%).

Each operator has precedence which determines in which order operators in an expression are evaluated. Operators with lower-precedence numbers has higher precedence and are evaluated before higher precedence numbers. Parentheses have the highest-precedence, so they can be used to override precedence order. Operators with the same precedence number are evaluated from left to right in the order they appear.

...
Table 4.1
Example 1
  • Table 4.2
    Expression Explanation
    13 + 8 / 4 – 2 * 6 Initial Expression
    13 + 2 – 2 * 6 Division is performed first
    13 + 2 – 12 Multiplication is performed next
    15 – 12 Addition is performed next
    3 Subtraction is performed last
Example 2
  • Table 4.3
    Expression Explanation
    13 + 8 / (4 – 2) * 6 Initial Expression
    13 + 8 / 2 * 6 subtraction is performed first
    13 + 4 * 6 Division is performed next
    13 + 24 Addition is performed last
    37 Subtraction is performed last

Increment and Decrement Operators

Unary operator (one operand) and subtraction is a binary operator (two operands).

Table 4.4
Name Example Explanation
Pre-increment ++x Adds 1 to the existing value.
Same as: x = x + 1
Pre-decrement --x Subtracts 1 to the existing value.
Same as: x = x - 1
Post-increment x++ Adds 1 to the existing
Same as: x = x + 1
Post-decrement x-- Subtracts 1 to the existing value.
Same as: x = x - 1

If prefix form is used then increment or decrement will be done before the rest of the expression, and if postfix form is used then increment or decrement will be done after the complete expression is evaluated.

Example

What is the value of x and y after the each of the statements is executed? Assume the values of x and y is 6 and 10, respectively, before each statement is executed.

Example: What is the value of x and y after the each of the statements is executed? Assume the values of x and y is 6 and 10, respectively, before each statement is executed.
  • Table 4.5
    Expression Result Explanation
    x = --y x = 9
    y = 9
    The value of y is decremented first before being assigned to x)
    x = y++ + 2 x = 12
    y = 11
    The value of y is used in the expression and then it is incremented at the end.
    y *= x-- x = 5
    y = 60
    This is same as y = y * x--.
    The value of x is used in the expression and it is decremented at the end.)

Assignment Statements

An assignment statement is used to assign a value to a variable while a program is running.

Syntax:
  1. variableName = expression;
  • The equal (=) symbol is the assignment operator which tells the computer to evaluate the expression on the right side of the assignment operator and store the result in the variable on the left side of the operator.
  • expression can include one or more literal constants, named constants, variables, or arithmetic operators.
  • The data type of expression in an assignment statement must match the data type of the variable. If they don’t match, the compiler will use implicit type casting to get them to match. It’s better to explicitly cast to correct data type yourself.

A assignment operator can be abbreviated as well. Statement must be of the form variableName = variableName arithmeticOperator value, which is abbreviated as variableName arithmeticOperator= value . Most common operators are += , -= , *= , /= , and %=. Table 4.6 shows the expression and abbreviations of it.

Table 4.6
Expression Abbreviated Expression
a = a + b; a += b;
a = a - b; a -= b;
a = a * b; a *= b;
a = a / b; a /= b;
a = a % b; a %= b;

Relational Operators

Relational (also called Comparison operators) are used to compare two values that have the same data type. Table 4.7 shows the relational operators used and its meaning.

  • Table 4.7
    Operator Name Example Precedence No.
    < Less than height < 180 1
    <= Less than or equal to age <= 18 1
    > Greater than mark > 50 1
    >= Greater than or equal to temp >= 90 1
    == Equal to grade == 90 2
    != Not equal to response != 2 2

Relational expressions are evaluated to a numerical value of 1 or 0 only. If the value is 1, the expression is true. If the value is 0, the expression is false. It is adviseable that not to use equality (==) or inequality operator (!=) to compare real numbers. Real numbers cannot be stored exactly, therefore, test that absolute value of their difference is within some small threshold. char values are automatically coerced to int values for comparison purposes. Strings are compared on a character by character basis. The string with the first lower character is considered smaller

Example 1
  • Table 4.8
    Expression Explanation
    7 – 3 + 8 < 9 + 5 Initial Expression
    4 + 8 < 9 + 5 Subtraction is performed first
    12 < 9 + 5 First addition is performed next
    12 < 14 Second addition is perfomed next
    true Less then operator is performed last
Example 2
  • ...
    Table 4.9

Logical Operators

Logical operators allow the combining of two or more conditions (sub-conditions) into one compound condition. Also called Boolean operators and always evaluate to true or false.

The two most common are And (&&) and Or (||). All sub-conditions must be true for a compound condition using And to be true. Only one of the sub-conditions must be true for a compound condition using Or to be true. And is evaluated before Or in an expression.

Logical operators are evaluated after arithmetic and comparison operators. Parentheses can override precedence ordering.

Truth tables summarize how computer evaluates logical operators. Only necessary sub-conditions are evaluated; called short-circuit evaluation . For example: if the first sub-condition in an And clause is false, the second sub-condition need not be evaluated.

Table 4.10
Truth Table for And (&&) Operator
Subcondition1 Subcondition2 Subcondition1 && subcondition2
True True True
True False False
False True False
False False False

Table 4.11
Truth Table for Or (||) Operator
Subcondition1 Subcondition2 Subcondition1 && subcondition2
True True True
True False True
False True True
False False False

Table 4.12
Truth Table for Or (||) Operator
Subcondition !Subcondition2
True False
False True

Example 1: sales > 3500 && sales < 6000
  • The compound condition is evaluated to be true when the number stored in the sales variable is greater than 3500 and at the same time, less than 6000; otherwise it evaluates to false.
Example 2: measurement == 20 || measurement > 65
  • The compound condition evaluates to true when the number stored in the measurement variable is either equal to 20 or greater than 65; otherwise, evaluates to false.

Table 4.13
Operator Operation Precedence No.
( ) Parenthesis overrides normal precedence rules 1
! Negation (reverses the sign of a number) 2
*, /, % Multiplication, division, and modulus 3
+, - Addition and subtraction 4
<, <=,>, >= Less than, less than or equal to, greater than, greater than or equal to 5
==, != Equal to, not equal to 6
And (&&) All sub-conditions must be true for the compound condition to evaluate to true 7
Or (||) Only one sub-condition needs to be true for the compound condition to evaluate to true 8

Example
  • Table 4.14
    Expression Explanation
    20 < 80 / 2 + 3 && 25> 10 * 2 Initial Expression
    20 < 40 + 3 && 25> 10 * 2 80 / 2 is performed first
    20 < 40 + 3 && 25> 20 10 * 2 is performed next
    20 < 43 && 25> 20 40 + 3 is perfomed next
    True && 25 > 20 20 < 43 is perfomed next
    True && True 25 > 20 is perfomed next
    True True && true evaluates to true