Professional Documents
Culture Documents
04 Primitive Operations
04 Primitive Operations
James Brucker
Arithmetic Operators
Arithmetic operators:
-b Negation
a*b Multiplication
a/b Division.
a % b Remainder of a / b, may be negative
a + b Addition
a-b Subtraction
a + b * c Multiplication then addition
a = 3; a=3
x = 4 + 2 * 9 / 6 / a - 1; x = 4 + (18/6)/3 - 1 = 4
y = 2 + 12 * 2 / 6 % a - 1; y = 2 + (24/6)%3 - 1 = 3
Quiz on Order of Operations
What are the resulting values for the following?
a = 4; b = 12; c = 4; d = 2;
n1 = a + b * c + d; n1 =
n2 = a + b * (c + d); n2 =
n3 = b / a * c / d; n3 =
n4 = b / a + c / d; n4 =
n5 = -a + 15 % c - d; n5 =
Type of Results
What is data type of the result of an operation?
Examples: what is...
15 * 200 = 3000 (int)
15F * 200F = 3000F (float)
0.5 * 7.0 = 3.5 (double)
7/2 = 3 (int) ... NOT 3.5
Rule: For primitive numeric types, the result of + - * / % is the same type!
a b a op b - a
int int int int
longlonglonglong
floatfloatfloatfloat
double double double double
How is Arithmetic Done?
QUESTION:
Does the CPU have hardware instructions for + - * /
involving integer data, or does it use software?
Does the CPU have hardware instructions for + - * /
involving float point data?
What is the name of the CPU component that performs
+-*/?
int
is multiplication done using MULT result
hardware or software?
int
Arithmetic and Type Conversion
The BIG Question:
The CPU can't directly add int + double or int * double,
because they are different types.
int double
double int
Arithmetic and Type Conversion (1)
Operations are defined for each data type
When Java performs arithmetic (+ - * / %) on two
int Conversion byte -> char, char -> int is mostly for
I/O involving character data. Be careful!
short,char
Widening Conversions
byte These promotions are called widening
conversions because the higher data types have
larger ("wider") range of possible values.
Automatic Conversions
The widening conversions are easy to remember if you
remember the size & range of each data type:
a 100
short a = 100;
int b = 1000; b
float x = 2E+30; x mantissa exp
b = a; no problem: b can store any "short" value
x = a; no problem: x has store any "short" value
a = b; error! a is too small to store all "int" values
b = x; error! b cannot store some large "float"
values
Automatic Conversions (2)
Value Can be converted and assigned to:
byte short, int, long, float, double
short int, long, float, double
int long, float, double
long float, double
float double
float x, y;
x = 100; // OK. Integer 100 can be converted to "float"
y = 0.5 * x; // Error! "0.5" is a double, so the result is a double
y = 0.5F * x;// OK. Both operands are float, so result is float
Examples
Expression Result
15 / 2 7 ( int )
15 / 2.0F 7.5F (float)
15 / 2.0 7.5 (double!)
int VAT = 7; // tax rate
5000 * ( 1 + VAT/100) 5000 (no tax!)
5000 * ( 1 + VAT/100.) 5350. (tax)
int a; float x; double d; 0
x = 3.14159; Error: float <-- double
a = 2.5F * x; Error: int <-- float
d = 123456789011121314L; OK: double <-- long
a = Math.sqrt( 2 ); Error: int <-- double
count = 1
count = 2
count = 3
Done. count = 4
Increment/Decrement Operators (3)
Increment is also used in counting things, like this:
a = 5; x = y = 5;
k1 = a++; n1 = x++ * y--;
k2 = ++a; n2 = ++x * y--;
n3 = x++ * --y;
n4 = ++x * --y;
Expression Meaning
sum += x; sum = sum + x;
sum -= x; sum = sum - x;
prod *= x; prod = prod * x;
prod /= x; prod = prod / x;
prod %= x; prod = prod % x;