Operators and
Expression
Rakib Mahmud
Operator & Operand
• An operator can be defined as the symbol that helps us
to perform some specific mathematical, relational,
bitwise, conditional, or logical computations on values
and variables.
• The variable and constants on which the operation tag
are known as operand.
Example
Expression
Operators ( =, + )
c = a + b;
Operands (a,b,c)
Types of Operators
• Arithmetic operators
• Assignment operators
• Unary operators
• Relational operators
• Logical operators
• Conditional operators
• Bitwise operators
Arithmetic operators
Operator Description Example Result
+ Do addition x = 15 + 6 x = 21
- Do subtraction x = 15 - 6 x=9
* Do multiplication x = 15 * 6 x = 90
/ Do division x = 15 / 6 x=2
% Do modular division x = 15 % 6 x=3
These operators can be applied on any type of data except Boolean.
Arithmetic operators (Cont’d)
Arithmetic operators (Cont’d)
Type casting need to be done
Arithmetic operators (Cont’d)
• % operator works on float and double data types.
Arithmetic operators (Cont’d)
• To write down the expression one should be familiar
with the precedence and associativity of operators.
• *, /, % have higher precedence than +, -.
• To change the precedence of operators () are used.
Writing expressions:
x=
x
Datatypes of Expression
• byte b = 10; 1. X = b+ 1. Int
• short s = 15; s;
2. Int
• int i = 7; 2. X = s+ 3. Float
i; 4. Float
• long l = 50l;
3. X = i+ 5. Int
• float f = 12.5f; f; 6. long
• double d = 17.5d; 4. X = l+ 7. Double
• char c=65; f; 8. Double
5. X = c+
s;
6. X = c+
Datatypes of Expression
• The addition, subtraction, multiplication or division between
the byte, short and int results is int as they belong integer
type of data.
byte + byte
• The mechanism of converting data type internally by the
compiler is known as “COERCION”.
• If float is used in the expression, then the result is float.
• If Double is used in the expression, then the result is double.
• When char is used the result is int type as char is part of
int(codes).
Datatypes of Expression
Datatypes of Expression
Assignment Operator
• Assignment operators are used for assigning value to a
variable.
Operator Example Full Meaning
+= x+=5 x = x +5
-= x-=5 x=x-5
*= x*=5 x=x*5
/= x/=5 x=x/5
%= x%=5 x=x%5
Assignment Operator
Unary Operators
• Unary Operators need only one operand.
• Unary Minus (-) : Negates the value.
• Unary Plus (+) : Indicates a positive value
• Increment Operator (++) : Increments by 1.
• Pre-Increment: Increments first, then uses value.
• Post-Increment: Uses value first, then increments.
• Decrement Operator (--) : Decrements by 1.
• Pre-Decrement: Decrements first, then uses value.
• Post-Decrement: Uses value first, then decrements.
• These operators cannot be applied to Boolean type of
data.
Unary Operators (Cont’d)
Relational Operator
• A relational operator checks the relationship between
two operands.
• Relational operators are used in decision
making and loops.
• The outcome of these operation is a boolean value.
Relational Operator (Cont’d)
Operator Meaning of Operator Example
5 == 3 is evaluated to
== Equal to
false
> Greater than 5 > 3 is evaluated to true
< Less than 5 < 3 is evaluated to false
!= Not equal to 5 != 3 is evaluated to true
5 >= 3 is evaluated to
>= Greater than or equal to
true
5 <= 3 is evaluated to
<= Less than or equal to
false
Relational Operator (Cont’d)
Logical Operator
• An expression containing logical operator returns either
true or false depending upon the evaluation of
expression.
• Logical operators are commonly used in decision
making.
Logical Operator (Cont’d)
Operator Meaning Example
If c = 5 and d = 2 then,
Logical AND. True only if
&& expression ((c==5) &&
all operands are true
(d>5)) equals to false.
If c = 5 and d = 2 then,
Logical OR. True only if
|| expression ((c==5) ||
either one operand is true
(d>5)) equals to true.
Logical NOT. True only if If c = 5 then, expression !
!
the operand is 0 (c==5) equals to true.
Logical Operator (Cont’d)
Conditional Operator
• The conditional operator is also known as a ternary
operator.
• It is represented by two symbols, i.e., '?' and ':’.
• As conditional operator works on three operands, so it is
also known as the ternary operator.
• The behavior of the conditional operator is similar to the
'if-else' statement as 'if-else' statement is also a
decision-making statement.
• Syntax :
Expression1? expression2: expression3;
Conditional Operator
Conditional Operator (Cont’d)
Bitwise Operators
Operators Meaning of operators
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise Not
<< Left shift
>> Right shift
>>> Unsigned right shift
Works with only integer type of data
Bitwise AND Operator &
• The output of bitwise AND is 1 if the corresponding bits
of two operands is 1. If either bit of an operand is 0, the
result of corresponding bit is evaluated to 0.
Bitwise OR Operator |
• The output of bitwise OR is 1 if at least one
corresponding bit of two operands is 1.
Bitwise XOR (exclusive OR) Operator
^
• The result of bitwise XOR operator is 1 if the
corresponding bits of two operands are opposite.
• It is denoted by ^.
Bitwise Complement Operator ~
• Bitwise complement operator is a unary operator (works
on only one operand).
• It changes 1 to 0 and 0 to 1.
• It is denoted by ~.
• For any integer n, bitwise complement of n will be -(n +
1)
35 = 00100011 ( In binary)
00100011 ~35 = 11011100
X = b12’s + 1
-X = b2 00100100 (36)
Left Shift Operator
• Left shift operator shifts all bits towards left by a certain
number of specified bits. It is denoted by <<.
• The bit positions that have been vacated by the left
shift operator are filled with 0.
• If the value is left shifted one time, then its value gets
doubled.
• For example, a = 10, then a<<1 = 20
10 = 00001010
10<<1 = 00010100
= 20
Right Shift Operator
• Right shift operator shifts all bits towards right by
certain number of specified bits. It is denoted by >>
• If the number is negative, then 1 is used as a filler and if
the number is positive, then 0 is used as a filler.
• If the value of a variable is right shifted one time, then
its value becomes half the original value.
• For example, a = 10, then a>>1 = 5
10 = 00001010 -10 = 11110110
10>>1 = 00000101 10>>1 = 11111011
=5
Unsigned Right Shift Operator
• In Java, the operator ‘>>>’ denotes unsigned right shift
operator and always fill 0 irrespective of the sign of the
number.
• For example, a = -10, then a>>>1 = 123 (Considering
only 8 bit)
-10 = 11110110
10>>1 = 01111011
= 123
Bitwise Operator (Cont’d)
Thank you