Professional Documents
Culture Documents
CSC 112 - Java perators week 5-2
CSC 112 - Java perators week 5-2
CSC 112 - Java perators week 5-2
Java
(CSC 112)
1
O. O. Java Expressions and Operators
2
O. O. Java Expressions and Operators
ØOperators are symbols that tell the compiler to perform certain mathematical or logical manipulations
ØOperators are defined as construct used to manipulate the values of operands
ØThey are used in programs to manipulate the primitive data types and variables
ØJava operators can be unary, binary or ternary
Ø Example: unary ++x;
Ø Binary: x+ y;
Ø Ternary: (x>y)? x: y;
3
O. O. Java Expressions and Operators
4
O. O. Java Expressions and Operators
ØThese include unary operators (+, -), unary increment and decrement (++,--), multiplication (*), division (/),
modulo (or remainder), addition and subtraction
ØThese operators operate on any of the built-in numeric data types like byte, short, char, int, long, float nd
double
ØThe operators can not be used on Boolean data type
ØWhen the binary operators are used to combine two operands (values or expressions); the values must be of
the same data type
ØExample: int x = 6 + 7;
Ø if the values are of different data types, the value of one of the operand will be converted implicitly to the data
type of the other operand, before the operation will be performed.
ØExample: float x = 6.5f + 44;
ØThis is achieved through typecasting
6
O. O. Java Expressions and Operators
Ø When arithmetic expressions are used on either side of a relational operator, the arithmetic expressions will
be evaluated first and then the results compared.
Ø That is arithmetic operators have a higher priority over relational operators.
8
O. O. Java Expressions and Operators
Ø Logical operators are used to join two or more relational operations to form complex expressions.
Ø Java logical operators include
Ø logical NOT operator (!)
Ø Conditional OR operator (||) (Short circuit OR Operator)
Ø Conditional AND operator (&&) (Short circuit AND Operator)
Ø Logical OR operator (|)
Ø Logical AND operator (&)
Ø Logical exclusive OR operator (^)
Ø Example: logical AND
Ø {int x = 6; int y = 8;
Ø System.out.println((x>y) & (x<y));}
10
O. O. Java Expressions and Operators
Ø Java provides several compound assignment operators for abbreviating assignment expressions. The
compound assignment operator is also called augmented assignment operator. Any statement of the form
Ø Variable = <variable> <operator><expression>;
Ø Where
Ø operator:: +|-|*|%|/|>>|<<|^|etc
Ø Can be written in the compound assignment operator form as:
Ø <Variable><operator> =<expression>;
Ø For example, you can abbreviate the statement c = c +3; with the addition compound assignment operator,
+=, as
Ø c += 3;
11
O. O. Java Expressions and Operators
Ø Compound assignment operators require two operands: left and right operands. The left operand must be a
variable and the right operand can be a constant, variable or an expression. An expression consists of a
sequence of operands and operators that specify the computation of a value. The right-side expression is
solved first before applying the compound assignment operator.
Ø For example, the statement:
Ø public static void main(String[] args) { Assignment operator Sample expression Explanation Assignment result
Assume int c=3, d=5, e=4, f = 6, g = 12;
Ø // TODO code application logic here += c += 7 c = c + 7; 10 is assigned to c
-= d -= 5 d = d-4; 1 is assigned to d
Ø int x=12; *= e *=4 e=e*5 20 is assigned to e
12
O. O. Java Expressions and Operators
Ø Java provides two types of unary increment and decrement operators for adding 1 to a numeric variables or
subtracting one from the numeric variable.
Ø That is ++x means x = x+1;
--x means x = x -1;
Ø The unary increment and decrement operators are divided into prefix and postfix increment and decrement
operators.
Ø A prefix increment and decrement operators increases or decreases the of the variable by 1 before the variable
is used in computations
Ø Example:
Ø { int x = 4; int y = ++x * 2; System. out.println(y)}
Ø This will output the value of y as 10.
Ø The postfix increment and decrement operators use the variable value first in computation before the variable
is either increased by 1 or decreased by 1. 13
O. O. Java Expressions and Operators
Ø The expression (5>4) returns true > > x>y x is greater than y
< < x<y x is less than y
Ø The expression (3!=2) returns true >= >= x >= y x is greater than or equal to y
Ø If given a = 2, b = 3 and c = 4; then the expressions <= <= x <= y x is less than or equal to y
14
O. O. Java Expressions and Operators
Ø The bitwise operators are used to manipulate data at the individual bit level.
Ø The bitwise operators include:
Operator Description Example Answer
Ø Examples: ~ Bitwise not operator ~4 -5
<< Bitwise shift left operator 4<<2 16
Ø { int x = -4; System.out.println(x);} >> Bitwise shift right operator 4>>2 1
>>> Unsigned right shift operator 4>>>2 1
Ø { System.out.println(~4);} & Bitwise AND operator 3&4 0
^ Bitwise Exclusive OR 3 ^4 7
Ø { int x = 4; int y = 6; System.out.println(x|y);}
operator
•Examples: Assume A = 60 and B =13 then | Bitwise OR operator 3 |4 7
15
O. O. Java Expressions and Operators
1 1 0 1 1 0
1 0 0 0 1 1
0 1 1 0 1 1
0 0 1 0 0 0
16
O. O. Java Expressions and Operators
Ø The comma operator is used for multiple or complex declarations of variables of the same data type.
Ø For instance, to declare three variables a, b, and c of type int, we have:
Ø int a, b,c;
Ø this can be written as:
• int a; int b; int c;
•
17
O. O. Java Expressions and Operators
Ø The ternary operator, represented by (?:), consists of three operands Java includes a special ternary (three-
way) operator that can replace certain types of if-then-else statements.
Ø The ternary operator represented by the symbol (?:) consists of three operands. The general form of the
ternary operator (?:) is:
<Variable> = (Condition)? expression1 : expression2;
Here, condition can be any expression that evaluates to a Boolean value. The statement states that if the
condition is true, then expression1 gets evaluated and executed; otherwise, expression2 gets evaluated and
executed The final result of the ternary (?:) operation is stored in a variable. Both expression1 and
expression2 are required to return the same data type, which can’t be void.
Example: Given x = 12 and y = 6; evaluate the conditional statement
{ int M = (x<y? x: y);
System.out.println(M);} 18
19
THANK YOU
THE END