Operators

Overview

Operators

the 5 Arithmetic Operators

  1. = assignment
  2. + addition
  3. - subtraction
  4. * multiplication
  5. / division
  6. % modulo

Assignment Operator =

Addition Operator +

int augend = 5;
int addend = 10;
int sum = augend + addend; // `sum` is 15

Subtraction Operator -

int minuend = 15;
int subtrahend = 10;
int difference = minuend - subtrahend; // `difference` is 5

Multiplication Operator *

int multiplicand = 5;
int multiplier = 10;
int product = multiplicand * multiplier; // `product` is 50

Division Operator /

int dividend = 10;
int divisor = 5;
int quotient = dividend + divisor; // `quotient` is 2

Modulo Operator %

int dividend = 10;
int divisor = 5;
int remainder = dividend + divisor; // `quotient` is 2

the 6 Logical Operators

  1. ! not
  2. && conditional and
  3. || conditional or
  4. ^ xor (exclusive or)
  5. & and
  6. | or

the unary ! operator

boolean rightOperand = true;
boolean outcome = !rightOperand; // outcome is `false`
boolean rightOperand = false;
boolean outcome = !rightOperand; // outcome is `true`

the binary & operator

boolean canWalkThroughDoor = door.isUnlocked() & door.isOpen();
boolean doorIsUnlocked = door.isUnlocked();
boolean doorIsOpen = door.isOpen();
boolean canWalkThroughDoor = doorIsUnlocked && doorIsOpen;

the binary && operator

Example
boolean canWalkThroughDoor = door.isUnlocked() && door.isOpen();
boolean canWalkThroughDoor;
if(door.isUnlocked()) {
    if(door.isOpen()) {
        canWalkThroughDoor = true;
    }
}

the binary | operator

boolean canWalkThroughDoor = door.isUnlocked() | door.isOpen();
boolean doorIsUnlocked = door.isUnlocked();
boolean doorIsOpen = door.isOpen();
boolean canWalkThroughDoor = doorIsUnlocked || doorIsOpen;

the binary || operator

the binary || operator

Example

boolean canWalkThroughDoor = door.isUnlocked() || door.isOpen();
boolean canWalkThroughDoor;
if(door.isUnlocked()) {
    canWalkThroughDoor = true;
}
if(door.isOpen()) {
    canWalkThroughDoor = true;
}

Relational Operators

the 6 Relational Operators

  1. < less than
  2. > greater than
  3. <= less than or equal to
  4. >= greater than or equal to
  5. == equal to
  6. != not equal to

Relational Operators

< Less than

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

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

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

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

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

int startingValue = 10;
startingValue++; // startingValue is `11`

+= Increment Operator

int startingValue = 10;
startingValue += 5; // startingValue is `15`
int startingValue = 10;
startingValue = startingValue + 5; // startingValue is `15`

-- Decrement Operator

-=, Decrement Operator

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

4 + 5 * 8 == 44
(4 + 5) * 8 == 72

Since += associates right to left, the expression

a += b += c

means

a += (b += c)