Professional Documents
Culture Documents
Kinds of Operator
Precedence of Operator
Type Casting
Operator
x = -5 ;
x = -(-5) ;
x = -(3-5) ;
[ArithmeticOperators.java]
Arithmetic Operator
Infinitive arithmetic
java.lang.Float, java.lang.Double,
POSITIVE_INFINITY, NEGATIVE_INFINITY constant
NaN(Not a Number)
Arithmetic Operator
Add, Substract
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
Multiply, Divide
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
[RelationalOperators.java]
Conditional Operator
[LogicalOperators.java]
Increment & Decrement Operator
Operator
++, --
Prefix operator
n = 1;
x = ++n; // x=2, n=2
Postfix operator
n = 1;
x = n++; // x=1, n=2
Operator
&, |, <<, >>, >>>, ^, ~
Operand should be integer type
Precedence
Operator Precedence
~ (H)
<< >> >>>
&
^
| (L)
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
Shift right(>>)
x >> y = x / 2y
[ShiftOperators.java]
The Conditional Operator
Operator
Expr1 ? Expr2 : Expr3 (3 Terms Operator)
if (x > y) max = x;
max = x > y ? x : y ; else max = y;
[ConditionalOperator.java]
[PrintTenItem.java]
Assignment Operators
Operator
Arithmetic operator : + - * / %
Bitwise operator : & | ^ << >> >>>
x = x * y + 1; x *= y + 1;
[AssignmentOperators.java]
x = x * (y+1)
Cast Operator
(Data Type) 식
Cast operator : ( , )
[CastOperator.java]
Operator Precedence
Operator Association Precedence
() [] . Left Assoc. (High)
! ~ ++ -- + - (Data Type) Left Assoc.
* / % Left Assoc.
+ - Left Assoc.
<< >> >>> Left Assoc.
< <= > >= instance Left Assoc.
== != Left Assoc.
& Left Assoc.
^ Left Assoc.
| Left Assoc.
&& Left Assoc.
|| Left Assoc.
?: Left Assoc.
= += -= *= /= %= &= ^= |= <<= >>= >>>= Left Assoc. (Low)
Operator Precedence
Examples :
byte -> short, int, long, float, double
short -> int, long, float, double
char -> int, long, float, double
int -> long, float, double
long -> float, double
float -> double
[WideningTypeConversion.java]
Type Conversion
Examples :
(1) i = (c + s); // i = ??
(int T) (char T) (short T)
(short T)
(int T)
[LowerToUpperConversion.java]
Type Conversion
Explicit type conversion
Converted by programmer using cast operator
char c='A';
short s=1; int i=2; long l=3;
float f=2.1f; double d=3.2;
(int T)
(short T)
[ExplicitTypeConversion.java] [LosePrecision.java]
Type Conversion
[Forbidden.java]