CSC 112 - Java perators week 5-2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Programming Language Using O. O.

Java
(CSC 112)

Module 4: Lesson Five


Expressions and Operators

1
O. O. Java Expressions and Operators

ØAn expression is a piece of program code that represents or computes a value.


ØAn expression can be:
Ø A literal like ‘A’ , ‘8’ , “Hello”
Ø Variable like x
Ø Value like 26
Ø A function call
Ø Combination of operators and variables or value like x + 6 *2;
ØMore complex expression can be assigned to a variable by using an assignment operator
Example: x = 5 * 5 + 6;

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

ØJava operators fall into nine (9) categories:


Ø Assignment operator
Ø Arithmetic operators
Ø Relational operators
Ø Logical operators
Ø Bitwise operators
Ø Augmented or compound assignment operators
Ø Conditional operators
Ø Comma operator
Ø Increment and decrement operators

4
O. O. Java Expressions and Operators

ØAssignment operator is used to assign a value or expression to variable


ØThe equality symbol (=) is used to assign a value or expression to a variable
ØThe syntax of assignment statement is:
Ø <variable> = <value>|<expression>
Ø Example: x = 6;
Ø x = 6 +6 *3;
ØIf a variable already exists with a value, its value is overwritten by the assignment statement
{ int c = 3; c = c + 2; System.out.printf(); }
ØIn general, the type of the expression on the right-hand side of an assignment statement must be of the same
data type as the type of the variable on the left-hand side
ØHowever, in some cases, the computer will automatically convert the value computed by the expression to
match the type of the variable.
5
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

ØExample: float x = 6.5f + 44;


ØThis is achieved through typecasting
ØWhen dividing an integer with another integer, the result will be an integer result. If the result has fractional
part or floating point value, the decimal part will be discarded.
ØExample: {int x = 7/3; System.out.println(x)}
ØThe arithmetic operators used in Java programming language include:
Operation(s) Operator(s) Order of evaluation (Precedence)

Brackets () For multiple brackets, innermost brackets is evaluated first.

Unary operator +, - Evaluated form right to left


Unary increment and decrement ++,— Evaluated form right to left. Does not accept multiple operators
operators
Multiplication * If there are several operators of these types, they are evaluated from left to
Division / right.
Remainder %
Addition + If there are several operators of these types, they are evaluated from left to
Subtraction - right.
Assignment = Evaluated last in the order of precedence 7
O. O. Java Expressions and Operators

Ø A condition is an expression that can be either true or false.


Ø Java control statements allow a program to make decisions based on the value of the condition.
Ø The relational and equality operators are used to evaluate a comparison between expressions.
Ø The result of a relational and equality operators is a Boolean value that can either be true or false.
Ø Condition in the if statement can be formed by using the equality operators (= = and !=) and relational
operators (>, <, >=, <=).

Ø The relational and equality operators associate from left to right.

Ø 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

Ø The relational operators are:


Standard algebraic Java equality or Simple Java Meaning of Java condition
equality or relational relational operator condition
operator
Equality operator
= == x= =y x is equal to y
!= != x != y x is not equal to y
Relational Operator
> > x>y x is greater than y
< < x<y x is less than y
>= >= x >= y x is greater than or equal to y

Ø Examples: <= <= x <= y x is less than or equal to y

Ø The expression (A>B) returns false


Ø The expression (5>4) returns true
Ø The expression (3!=2) returns true
Ø If given a = 2, b = 3 and c = 4; then the expressions
Ø (a * b >= c) is evaluated as (2 * 3 >= 4) = (6 >= 4) returns true
9
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

Ø x += 6+8*2; /= f /= 3 f = f/3 2 is assigned to f


%= g %= 9 g = g%9 3 is assigned to g
Ø System.out.println(x); >>= f>>=2 f = f>>2 1 is assigned to f
<<= f<<=2 f =f<<2 24 is assigned to f
Ø ^= f ^= 2 f = f^2 4 is assigned to f
|= f |= 2 f = f|2 4 is assigned to f

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

Standard algebraic Java equality or Simple Java Meaning of Java condition


equality or relational relational operator condition
Ø The relational operators are: operator
Equality operator
Ø Examples: = == x= =y x is equal to y

Ø The expression (A>B) returns false != != x != y x is not equal to y


Relational Operator

Ø 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

Ø (a * b >= c) is evaluated as (2 * 3 >= 4) = (6 >= 4) returns true

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

A & B = 60 & 30 = (00111100) & (00001101) = 000011002 = 12


A | B = 60 | 30 = (00111100) | (00001101) = 001111012 = 61
A ^ B = 60 ^ 30 = (00111100) ^ (00001101) = 001100012 = 49
B>>2 = 30>>2= (00001101>>2) = 000000112 =3
~B = ~13 = (2n+1 – 1)- (1101) = (25-1) -1101 = (100000-1) -1101
= 11111 -1101 = 10010

15
O. O. Java Expressions and Operators

The Truth Table for the Bitwise operators is illustrated as follows:

P Q ~P P&Q P|Q P^Q

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

You might also like