You are on page 1of 23

Operator

Kinds of Operator
Precedence of Operator
Type Casting
Operator

Decide the semantics of expression

Meaning of operator given in language system


Kinds of Operator
Arithmetic Op. : + - * / %
Relational Op. : > >= < <= == !=
Logical Op. : && || !
Inc/Dec Op. : ++ --
Operators of Java Bit Op. : & | ^ ~ << >> >>>
Conditional Op. : ?:
Assign Op. : = += -= *= /= %= &= ^= |= >>= <<= >>>=
Casting Op. : (Data Type)
Array Op. : []
Method Op. : () .
instanceof Op. : instanceof
Arithmetic Operator

Operator for arithmetic operation


Single term operator : +, -
Binary term operator : +, -, *, /, %

x = -5 ;
x = -(-5) ;
x = -(3-5) ;

 [ArithmeticOperators.java]
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
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

Compare two value


Result : true or false
Expression include relational operator
for, while, ...
Operator
, =, , =, ==, = a > b + c ===> a > (b + c)
b == x < y ===> b == (x < y)
 precedence 

 [RelationalOperators.java]
Conditional Operator

Conditional Logical Relationship of two


operands
Operator
! , && , ||

a < b && b < c


1 2

 [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

Cannot use at expression, only at variable


Cannot apply at real type
(a + b)++ // error
 [IncDecOperators.java]
Bitwise Operator

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

Bitwise Shift Operator


Shift lefe(<<)
x << y = x * 2y

Shift right(>>)
x >> y = x / 2y

Unsigned shift right(>>>)


 Give this operator because Java does not support unsigned integer.

 [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]

m = a > b ? (c > a ? c : a) : (c > b ? c : b) ;

 [PrintTenItem.java]
Assignment Operators

Expr 1 = Expr 1 op Expr2 Expr1 op= Expr 2

Operator
Arithmetic operator : + - * / %
Bitwise operator : & | ^ << >> >>>

sum = sum + i ; sum += i ;

x = x * y + 1; x *= y + 1;

 [AssignmentOperators.java]
x = x * (y+1)
Cast Operator

Data Type Casting Operator

(Data Type) 식

Cast operator : ( , )

(int) 3.75 ===> 3


(float) 3 ===> 3.0
(float) (1 / 2) ===> 0.0
(float)1/2 ===> 0.5

 [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

a=x+y-z; // Left Association


b = -x ; // Right Association
c = -x++ ;
d = -++x ;
e = -x + z ;
Type Conversion

Size Direction of Data Type


Widening Type Conversion (Casting down)
 Smaller Data Type → Larger Data Type
Narrowing Type Conversion (Casting up)
 Larger Data Type → Smaller Data Type

Who will convert the type?


Implicit type conversion
 Carried out by compiler automatically
Explicit type conversion
 Carried out by programmer using casting
Type Conversion

Widening Type Converstion


Implicit conversion by compiler automatically

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

Narrowing Type Conversion


Programmer should describe the conversion explicitly

Examples :

byte -> char


short -> byte, char
char -> byte, short
int -> byte, short, char
long -> byte, short, char, int
float -> byte, short, char, int, long
doule -> byte, short, char, int, long, float
Type Conversion

Implicit Type Conversion


Converted by compiler automatically
char c='A';
short s=1; int i=2; long l=3;
float f=2.1f; double d=3.2;

(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;

(1) s = (short) (c + i); // s = ??


(short T) (char T) (int T)

(int T)

(short T)

 [ExplicitTypeConversion.java] [LosePrecision.java]
Type Conversion

Type Conversion Prohibit


boolean type
 Only Can covert into same type

 [Forbidden.java]

You might also like