Operators
Overview
- Operators
- Arithmetic Operators
- Logical Operators
- Relational Operators
- Parenthesis and Operator Hierarchy
Operators
- An operator is a symbol which denotes a function to be done.
- An operator is followed by at least one operand.
- operands are the value (literal or variable) being operated on.
the 5 Arithmetic Operators
=
assignment+
addition-
subtraction*
multiplication/
division%
modulo
Assignment Operator =
- The assignment operator has two operands:
- The variable name
- The variable value
Addition Operator +
- The addition operator has two operands:
- augend is the base value
- addend is the value to add to the base value
- By adding values, we obtain a sum.
int augend = 5;
int addend = 10;
int sum = augend + addend; // `sum` is 15
Subtraction Operator -
- The subtraction operator has two operands:
- minuend is the base value
- subtrahend is the value to remove from the base value
- By subtracting values, we obtain a difference.
int minuend = 15;
int subtrahend = 10;
int difference = minuend - subtrahend; // `difference` is 5
Multiplication Operator *
- The multiplication operator has two operands:
- multiplicand is the base value
- multiplier is the value to remove from the base value
- By multiplying values, we obtain a product.
int multiplicand = 5;
int multiplier = 10;
int product = multiplicand * multiplier; // `product` is 50
Division Operator /
- The divison operator has two operands:
- dividend is the base value
- divisor is the value to remove from the base value
- By dividing values, we obtain a quotient.
int dividend = 10;
int divisor = 5;
int quotient = dividend + divisor; // `quotient` is 2
Modulo Operator %
- The modulo operator has two operands:
- dividend is the base value
- divisor is the value to remove from the base value
- By modding values, we obtain a remainder.
int dividend = 10;
int divisor = 5;
int remainder = dividend + divisor; // `quotient` is 2
the 6 Logical Operators
!
not&&
conditional and||
conditional or^
xor (exclusive or)&
and|
or
the unary !
operator
- Returns true if the operand to the right evaluates to false.
- Returns false if the operand to the right is true.
boolean rightOperand = true;
boolean outcome = !rightOperand; // outcome is `false`
boolean rightOperand = false;
boolean outcome = !rightOperand; // outcome is `true`
the binary &
operator
- Returns true if both of the operands evaluate to true.
- Both operands are evaluated before the
and
operator is applied. - The following code blocks have identical behavior in all cases
boolean canWalkThroughDoor = door.isUnlocked() & door.isOpen();
boolean doorIsUnlocked = door.isUnlocked();
boolean doorIsOpen = door.isOpen();
boolean canWalkThroughDoor = doorIsUnlocked && doorIsOpen;
- if
door
is not unlocked, then continue to evaluate if door is open
the binary &&
operator
- Expression resolves to
true
if both of the operands evaluate totrue
. - if the operand on the left returns
false
, the operation resolves tofalse
without evaluating the operand on the right.
Example
- The following code blocks have identical behavior in all cases
boolean canWalkThroughDoor = door.isUnlocked() && door.isOpen();
boolean canWalkThroughDoor;
if(door.isUnlocked()) {
if(door.isOpen()) {
canWalkThroughDoor = true;
}
}
the binary |
operator
- Returns
true
if at least one of the operands evaluates to true. - Both operands are evaluated before the
or
operator is applied.
boolean canWalkThroughDoor = door.isUnlocked() | door.isOpen();
boolean doorIsUnlocked = door.isUnlocked();
boolean doorIsOpen = door.isOpen();
boolean canWalkThroughDoor = doorIsUnlocked || doorIsOpen;
- if
door
is not unlocked, then continue to evaluate if door is open
the binary ||
operator
- Expression resolves to
true
if one of the operands evaluate totrue
. - if left operand is
true
, the operation resolves totrue
without evaluating the operand on the right.
the binary ||
operator
Example
- The following code blocks have identical behavior in all cases
boolean canWalkThroughDoor = door.isUnlocked() || door.isOpen();
boolean canWalkThroughDoor;
if(door.isUnlocked()) {
canWalkThroughDoor = true;
}
if(door.isOpen()) {
canWalkThroughDoor = true;
}
Relational Operators
- there are 6 relational operators that compare two numbers and return a boolean value.
the 6 Relational Operators
<
less than>
greater than<=
less than or equal to>=
greater than or equal to==
equal to!=
not equal to
Relational Operators
- Each operand is a numeric or boolean value.
- Boolean values are either true or false.
- The result of a relational operation is a boolean value.
<
Less than
true
if left operand is less than right operand, otherwisefalse
.
int operand1 = 0;
int operand2 = 1;
boolean outcome = operand1 < operand2; // outcome is `true`
int operand1 = 1;
int operand2 = 0;
boolean outcome = operand1 < operand2; // outcome is `false`
>
Greater than
true
if left operand is greater than right operand, otherwisefalse
.
int operand1 = 1;
int operand2 = 0;
boolean outcome = operand1 > operand2; // outcome is `true`
int operand1 = 0;
int operand2 = 1;
boolean outcome = operand1 > operand2; // outcome is `false`
<=
Less than or equal to
true
if left operand is less than or equal to right operand, otherwisefalse
.
int operand1 = 0;
int operand2 = 1;
boolean outcome = operand1 <= operand2; // outcome is `true`
int operand1 = 1;
int operand2 = 0;
boolean outcome = operand1 <= operand2; // outcome is `false`
int operand1 = 0;
int operand2 = 0;
boolean outcome = operand1 <= operand2; // outcome is `true`
>=
Greater than or equal to
true
if left operand is greatre than or equal to right operand, otherwisefalse
.
int operand1 = 0;
int operand2 = 1;
boolean outcome = operand1 >= operand2; // outcome is `false`
int operand1 = 1;
int operand2 = 0;
boolean outcome = operand1 >= operand2; // outcome is `true`
int operand1 = 0;
int operand2 = 0;
boolean outcome = operand1 >= operand2; // outcome is `true`
==
Equal to
true
if left operand is equal to right operand, otherwisefalse
.
int operand1 = 0;
int operand2 = 1;
boolean outcome = operand1 == operand2; // outcome is `false`
int operand1 = 1;
int operand2 = 0;
boolean outcome = operand1 == operand2; // outcome is `false`
int operand1 = 0;
int operand2 = 0;
boolean outcome = operand1 == operand2; // outcome is `true`
Increment / Decrement Operators
++
Increment Operator
++
adds 1 to the variable preceding the operator
int startingValue = 10;
startingValue++; // startingValue is `11`
+=
Increment Operator
+=
modifies the operand to the left to by adding the operand to the right to it.
int startingValue = 10;
startingValue += 5; // startingValue is `15`
int startingValue = 10;
startingValue = startingValue + 5; // startingValue is `15`
--
Decrement Operator
--
subtracts 1 from the variable preceding the operatorint startingValue = 10; startingValue--; // startingValue is `9`
-=
, Decrement Operator
-=
modifies the operand to the left to by subtracting the operand to the right from it.- the following blocks of code are behaviorally equivalent
int startingValue = 10;
startingValue -= 5; // startingValue is `5`
int startingValue = 10;
startingValue = startingValue - 5; // startingValue is `5`
Parentheses and Operator Hierarchy
Java order of operations
a && b || c
- means
(a && b) || c
4 + 5 * 8 == 44
(4 + 5) * 8 == 72
*
,/
have higher precedence than+
,-
Since += associates right to left, the expression
a += b += c
means
a += (b += c)