Arithmetic Operator
Operator for arithmetic operation
Single term operator : +, -
Binary term operator : +, -, *, /, %
[ArithmeticOperators.java]
x = -5 ;
x = -(-5) ;
x = -(3-5) ;
Arithmetic Operator
Real type operation
Floating point discription and operation: IEEE754
Standard
underflow, overflow
Infinitive arithmetic
java.lang.Float, java.lang.Double,
POSITIVE_INFINITY, NEGATIVE_INFINITY constant
NaN(Not a Number)
Arithmetic Operator
Add, Substract
Multiply, Divide
x y x+y x-y
+I.F +I.F +I.F NaN
+I.F - I.F NaN +I.F
- I.F +I.F NaN - I.F
- I.F - I.F - I.F NaN
NaN +I.F NaN NaN
x y x/y x%y
Finite 0.0 I.F NaN
Finite I.F 0.0
0.0 0.0 NaN NaN
I.F Finite I.F NaN
I.F I.F NaN NaN
[InfinityArithmetic.java]
Relational Operator
Compare two value
Result : true or false
Expression include relational operator
for, while, ...
Operator
, , , , ,
precedence
[RelationalOperators.java]
a > b + c ===> a > (b + c)
b == x < y ===> b == (x < y)
Conditional Operator
Conditional Logical Relationship of two
operands
Operator
! , && , ||
[LogicalOperators.java]
a < b && b < c
1 2
3
Increment & Decrement Operator
Operator
++, --
Prefix operator
Postfix operator
Cannot use at expression, only at variable
Cannot apply at real type
[IncDecOperators.java]
n = 1;
x = ++n; // x=2, n=2
n = 1;
x = n++; // x=1, n=2
(a + b)++ // error
Bitwise Operator
Bitwise AND
10012 & 00112 = 00012
To extract the special area in variable by masking that
area
Bit OR
10012 | 00112 = 10112
Exclusive AND
10012 ^ 00112 = 10102
1’s Complement
~ 000010102 = 111101012
[BitOperators.java]
Bitwise Operator
Bitwise Shift Operator
Shift lefe(<<)
Shift right(>>)
Unsigned shift right(>>>)
Give this operator because Java does not support unsigned integer.
[ShiftOperators.java]
x << y = x * 2y
x >> y = x / 2y
The Conditional Operator
Operator
Expr1 ? Expr2 : Expr3 (3 Terms Operator)
[ConditionalOperator.java]
[PrintTenItem.java]
m = a > b ? (c > a ? c : a) : (c > b ? c : b) ;
max = x > y ? x : y ;
if (x > y) max = x;
else max = y;
Assignment Operators
Operator
Arithmetic operator : + - * / %
Bitwise operator : & | ^ << >> >>>
[AssignmentOperators.java]
Expr 1 = Expr 1 op Expr2 Expr1 op= Expr 2
x = x * y + 1; x *= y + 1;
x = x * (y+1)
sum = sum + i ; sum += i ;