- Forward


Arithmetic Operators
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Review
Back SMYC Forward
  • Definitions:
    • An operator is a symbol indicating that an operation is to be performed on one or more operands
    • An operand can be a variable, literal, or expression
  • Number of Operands:
    • A unary operator has one operand
    • A binary operator has two operands
    • A ternary operator has three operands
  • Numeric Values:
    • Integer and floating-point numbers are both considered numeric values
Going Further: Operator Notations
Back SMYC Forward
  • Infix Notation for Binary Operators:
    • Operand Operator Operand
  • Postfix Notation for Binary Operators:
    • Operand Operand Operator
  • Prefix Notation for Binary Operators:
    • Operator Operand Operand
Going Further: Operator Notations (cont.)
Back SMYC Forward
  • Other Names:
    • Prefix Notation is sometimes called Polish Notation
    • Postfix Notation is sometimes called Reverse Polish Notation
  • Resulting Humor(?):
    • /imgs
      (Courtesy of xkcd)
Arithmetic Operators
Back SMYC Forward
  • Operands:
    • Numbers
  • Result:
    • A number
Binary Arithmetic Operators in Java
Back SMYC Forward
  • Numeric Operations and Operators:
    • Addition (+)
    • Subtraction (-)
    • Multiplication (*)
    • Division (/)
  • Operands:
    • Good practice says both operands should be of the same numeric type
    • In fact, Java will widen in some circumstances
  • Resulting Type:
    • When both operands have the same type then the result will also be of that type
Binary Arithmetic Operators in Java (cont.)
Back SMYC Forward
Examples in Assignment Statements
double area, price; int perimeter; area = 12.0 * 15.0; price = 45.00 / 2.0; perimeter = (300 * 2) + (160 * 2);

Review:
Can you identify all of the statements?
Can you identify all of the expressions? (Be careful!)

Binary Arithmetic Operators in Java (cont.)
Back SMYC Forward
  • Integer Operations and Operators:
    • Integer Division (/)
    • Remainder (sometimes called modulo) (%)
  • Operands:
    • int
  • Resulting Type:
    • int
Integer Division and Remainders
Back SMYC Forward
  • / (in Java):
    • Results in the int value that corresponds to the truncation of the real-valued quotient (i.e., the quotient rounded towards 0)
  • % (in Java):
    • The remainder after integer division, namely
      x - (x/y)*y
      where x is the dividend, y is the divisor, and / is the integer division operator
      (make sure you understand why x - (x/y)*y is not 0)
Integer Division and Remainders (cont.)
Back SMYC Forward
  • Gaining Experience with Integer Division:
    • 11 / 3
    • 6 / 3
    • 2 / 3
  • Gaining Experience with Remainders:
    • 11 % 3
    • 6 % 3
    • 2 % 3
Going Further: Integer Division and Remainders (cont.)
Back SMYC Forward
  • Two Options:
    1. Round to zero (i.e., truncation -- the result is closer to 0 than the quotient)
    2. Round toward negative infinity (i.e., floor -- the result is always less than or equal to the quotient)
  • Positive Numbers:
    • The two options yield the same result
  • Integer Division and Negative Numbers:
    1. -4/3 evaluates to -1
    2. -4/3 evaluates to -2
  • Remainder and Negative Numbers:
    1. 4 mod -3 evaluates to 1; -4 mod 3 evaluates to -1; -4 mod -3 evaluates to -1
    2. 4 mod -3 evaluates to -2; -4 mod 3 evaluates to 2; -4 mod -3 evaluates to -1
Going Further: Integer Division and Remainders (cont.)
Back SMYC Forward
  • The Two Options Repeated:
    1. Round to zero (i.e., truncation -- the result is closer to 0 than the quotient)
    2. Round toward negative infinity (i.e., floor -- the result is always less than or equal to the quotient)
  • The Two Options in Java (Starting in v8):
    1. / and %
    2. Math.floorDiv() and Math.floorMod()
Unary Numeric Arithmetic Operators in Java
Back SMYC Forward
  • Operations and Operators:
    • "Positive" (+)
    • Negate/Negative (-)
  • Operand:
    • A numeric type
  • Result:
    • The result of multiplying the operand by +1 or -1, respectively (without changing the type)
Unary Arithmetic Operators in Java (cont.)
Back SMYC Forward
  • Operations and Operators:
    • Increment (++)
    • Decrement (--)
  • Operand:
    • A numeric variable
  • Result:
    • The result of adding +1 or -1 to the operand, respectively (without changing the type)
  • Side Effect:
    • Increases or decreases the operand either before (in the case of ++x and --x) or after (in the case of x++ and x--) using it
Examples of Arithmetic and Assignment Operators
Back SMYC Forward
int age, grade, penalty; age = +21; ++age; penalty = -20; grade = 100 + penalty;
Precedence and Associativity
Back SMYC Forward
  • Precedence:
    • Controls the order in which operations are performed
  • Associativity:
    • Specifies the order in which operations of the same precedence are performed (whether left-to-right or right-to-left)
Precedence in Java (from High to Low)
Back SMYC Forward
  • Level 1:
    • ++(post) --(post)
  • Level 2:
    • ++(pre) --(pre) +(unary) -(unary)
  • Level 4:
    • * / %
  • Level 5:
    • +(binary) -(binary)
  • Level 15:
    • =
Associativity in Java
Back SMYC Forward
  • Binary Arithmetic Operators:
    • All have left-to-right associativity
  • Unary Arithmetic Operators
    • Most have right-to-left associativity
    • Post-increment and post-decrement have left-to-right associativity
Precedence/Associativity in Java
Back SMYC Forward

Gaining Experience

5 + 3 * 2

2 * 4 - 1

2 * (4 - 1)

2 + 8 + 3 - 2

8 * 4 * 2

8 / 4 / 2

A Good Practice
Back SMYC Forward
  • Motivation:
    • Numeric types in Java are unitless but expressions and statements often involve units
  • The Practice:
    • Include the units in a comment above the statement
A Good Practice (cont.)
Back SMYC Forward
Examples
double speed, distance, time; int area, width; int elapsed, hours, seconds; // mi/hr mi hr speed = distance / time; // in^2 in in area = width * width; // hr sec sec/hr hours = elapsed / 3600; // sec sec sec/hr seconds = elapsed % 3600;
A Practice to Avoid Revisited
Back SMYC Forward
  • An Example:
    • int i, j; i = 3 + (j = 5);
  • The Result:
    • 5 is assigned to j and 8 is assigned to i
  • Why it Should be Avoided:
    • In and of itself it's confusing
    • It can cause additional confusion in more complicated statements, especially those involving relational operators
A Similar Example
Back SMYC Forward
  • The Example:
    • int i, j; j = 0; i = 3 + j = 5; // Won't compile
  • Why It Won't Compile:
    • + has the highest precedence so 3 + j is executed first and evaluates to 3 (a value)
    • The assignments are then evaluated right to left. So, 5 is assigned to 3 and 3 isn't a variable
There's Always More to Learn
Back -