EXPRESSIONS IN JAVA
Expression is defined as the legal combination
of variables(operands) , constants and
operators.
Let us see an example
C= a + b * 4;
c is a variable or also called as operand
= is an assignment operator
a is an operand
+ is an operator
b is an operand
* is an operator
4 is a constant
CHAPTER - 5
OPERATORS AND EXPRESSIONS IN JAVA
Operator is basically a symbol or token ,
which performs arithmetical , relational
or logical operations and gives
meaningful result.
Types of operators
Operators
Relational logical Operators
Arithmetic operators
operators
Forms of Arithmetic operators
Forms of Arithmetic
Operators
Unary operator Binary operator Ternary Operator
Unary (+) +, - , *, /, % ?:
Unary (-)
Unary increment (++)
Unary decrement (--)
Unary Operators
An arithmetical operator which is applied to a single
operand is known as Unary operator.
Eg : +, -, ++, --
unary (+) is applied before the operand
i.e:
if a = 8 then + a will result in 8
If b= -16 then b store a value -16
this is all about unary (+) and unary (-)
Unary Operators
Unary increment and decrement operators
Unary increment operator (++) increases the value of
an operand by one .
Unary decrement operator (- -) decreases the value of
an operand by one.
Example:
X++ or ++ X means x=x+1
X - - or - - X means x = x-1
Unary Operators
Example:
++ X PREFIX INCREMENT OPERATOR
-- X PREFIX DECREMENT OPERATOR
PREFIX INCREMENT principle is “CHANGE BEFORE ACTION”
eg: p = 6;
p = ++p * 4;
the value will be ++p is 7
so the final result will be 28
Unary Operators
Example:
X ++ POSTFIX INCREMENT OPERATOR
X -- POSTFIX DECREMENT OPERATOR
PREFIX INCREMENT principle is “CHANGE AFTER THE
ACTION”
eg: p = 6;
p = p ++ * 4;
the value will be p ++ is 6
so the final result will be 24
Unary Operators
1. if p=5, find 4. if m=12;
d = ++p +5; then find
n= m++ * 5 + --m;
2. If a= 48, find 5. int a =0, b=30,c=40;
b = a++ + ++a; a= --b + c ++ + b;
3. If c =2, find
d= ++c + c++ +4;
Unary Operators
1. if p=5, find d 4. if m=12 find n
d = ++p +5; n= m++ * 5 + --m;
= 6+5 = 12 * 5 + 12
=11 = 60 +12
= 72
2. If a= 48, find b
1+1 5. int a =0, b=30,c=40;
b = a++ + ++a; a= --b + c ++ + b
= 48 + 50 = 29+ 40 +29
=98 = 98
3. If c =2, find
d= ++c + c++ +4;
= 3 + 3 +4
= 10
Binary Operators
• Binary operators are used to calculate.
• Binary operators are used between two operands.
• Binary operators returns the calculated value.
• Binary operators are:
• + used to find the sum
• - used to find the difference
• * used to find the product
• / used to find the quotient
• % used to find the remainder
Write the java expressions of the following
1. p=abc
2. R=2(l+b)
3. S=ab + bc + ca
4. 2(lb+bh+lh)
Expressions
1. p=a*b*c
2. R=2*(l+b)
3. S=a*b + b*c + c*a
4. 2*(l*b+b*h+l*h)
Shorthand Operator
• Shorthand operators are used to write an expression in a short
form for example.
z= z+y can be written as z+=y ie.
there are 6 shorthand operators +=, -=, *=, /=, %=
Shorthand operator is used when a variable is used as an
accumulator/counter in the expression , i.e. the same variable is to
be used both after and before the assignment sign
P++ means p= p+1 can also be written as p+=1
Counter Accumulator Shorthand
Problems on Shorthand Operator
i) If m=2 ,n=4 find the value of ii) if m=12 find n
n+= n++ + --m n= m++ *5 +--m
iii) If x=4, find iv) a=4,b=2,c=6
x-= x++ * ++x %2 boolean b=(a>b) ||! (a>c)
v) 6+8 >10 || 7<3+3 vi) !(7==7)
Problems on Shorthand Operator
iii) If x=4, find iv) a=4,b=2,c=6
x-= x++ * ++x %2 boolean b=(a>b) ||! (a>c)
=(4>2) ||!(4>6) = x- (x++ * ++x
%2) = T || !(F)
= 4 – (4 * 6%2) = T|| T
= 4 –( 4*0) =T
=4-0
=4
v) 6+8 >10 || 7<3+3 vi) !(7==7)
=14>10 || 7 <6 = !(T)
=T || F =F
=T
Relational Operators
• Relational operators are used to compare two
values.
• Relational operators returns boolean value.
• Relational operators are >=, <=, >, <, == ,!=.
• Example :
x=2 y=5
if (x > 5)
System.out.println( “x is greater”);
else
System.out.println( “x is smaller”);
Logical Operators
• Logical operators are used to combine two or more
expressions.
• Logical operators returns only boolean value.
• Logical operators are:
&& ( AND)
|| ( OR)
! ( NOT)
Example:
x=2 y =5
If (x>=y) &&(x==y) returns the value as
= (2>=5) && (2==5)
= F && F
= F
Truth table for AND and OR
AND OR
X Y X&&Y X Y X||Y
T T T T T T
T F F T F T
F T F F T T
F F F F F F
x=2 y =5
If (x>=y) &&(x==y) returns the
value as
= (2>=5) && (2==5)
= F && F
= F
Truth table for NOT
NOT
X !X
T F
F T
x=2 y =5
If !(x>=y) &&!(x==y) returns the value as
= !(2>=5) &&! (2==5)
= !(F) && !(F)
= T &&T
= T
DIFFERENCE BETWEEN BINARY THE OPERATORS
ARITHMETIC RELATIONAL LOGICAL
Compute or Compare Combine
Calculate
Returns a value Returns Boolean value Returns Boolean value
Eg: x>=y Eg: ( x>y ) && (x==y)
Eg: x +y
+,-,*,/,% >,<,>=,<= ,== ,!= || , &&, !
Ternary operator
Ternary operator deals with three operands
It is also called as conditional assignment
statement.
The ternary operator is ?:
for example:
To check the greatest among 2 numbers we
normally uses if statement ,here we can use
ternary operator also to do this. let us see……..
Ternary operator
If ( a>b)
g=a;
else
g=b;
int g = (a>b) ? a : b;
Rewrite the if statement into its
equivalent Ternary operator
1) if (marks >=90)
grade =“A”;
else
grade =“B”;
String grade= (marks >=90)?”A”: “B”;
Rewrite the if statement into its
equivalent Ternary operator
2) if (x%2 ==0)
ans= “even”;
else
ans = “odd”;
String ans =( x%2==0)? “even”:”odd”;
Rewrite the if statement into its
equivalent Ternary operator
3) If (income <= 100000)
tax=0;
else
tax= 0.1*income;
3)double tax = (income<=100000)?0:(0.1*income
Rewrite the if statement into its
equivalent Ternary operator
4) if (p>5000)
d= p*5/100;
else
d= p*2/100;
4) double d = (p>5000) ? (p*5/100):(p*2/100);
The dot operator
import java.io.*;
all the functions in the package
Input output dot operator
package
OUTPUT STATEMENT
dot
operator
System.out.println(); System.out.print()
object
Class function
Different forms of operator
Hierarchy of operators
Hierarchy of
Operators