5 - Operators and Expressions
5 - Operators and Expressions
CONTENTS
Operators
Types of Operators
Operator Precedence
Expressions in Java
Assignment Operator
New Operator
Dot Operator
Operator Precedence
Expressions in Java
Type Conversion
Implicit Type Conversion
Explicit Type Conversion
Mathematical Expressions in Java
Shorthand Notation of expressions
OPERATORS
An Operator is a symbol which performs operations such as arithmetical
and logical operations to provide meaningful results.
The values on which the operands perform operations are called as
Operands.
TYPES OF OPERATORS
Operators are classified into different types based on the type of operation
involved. There are four types of operators:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
TYPES OF OPERATORS
ARITHMETIC OPERATORS
Operators that are used to perform arithmetical calculations in a program are called as
Arithmetic Operators.
Based on the number of operands involved in the operation, the Arithmetic Operators
are further classified into two types. They are:
1) Unary arithmetic operators: An operator which is applied on a single operand.
Example: +, -, ++, -- +A
2) Binary arithmetic operators: An operator which is applied on two or more
operands.
Example: + , - , * , /, %
TYPES OF OPERATORS
Unary Arithmetic Operators
1. Unary ‘+’ – This operator is used as a prefix to the operand to specify the sign of the operand i.e. applied for positive
numbers.
Example: If a = 8, then +a = 8
2. Unary ‘-‘ - This operator is used as a prefix to the operand to specify the sign of the operand. It reverts the sign of an
operand.
Example: If a = 3, then –a will be -3
3. Unary Increment Operator ‘++’ – This operator increases the value of the operand by 1.
a) Unary Pre-increment operator – This is applied before the operand and works on the principle of “Change the
Value before the operation is performed”.
Example: If ‘a’ is the operand, then pre-increment operator applied on ‘a’ would result in ++a.
If a = 5, Evaluate p = 5+ ++a;
p = 5 + 6 = 11
b) Unary post-increment operator – This is applied after the operand and works on the principle of “Change
the value after the operation is performed”
Example: If ‘a’ is the operand, then post-increment operator applied on ‘a’ would result in a++
If a = 5, Evaluate p = 5 + a++;
p = 5 + 5 = 10, Value of ‘a’ will be stored as 6 after the operation is performed.
TYPES OF OPERATORS
Unary Arithmetic Operators
4. Unary Decrement Operator ‘- -‘ – this operator decreases the value of an
operand by 1.
a) Unary pre-decrement operator - This is applied before the operand and works on
the principle of “Change the Value before the operation is performed”.
Example: If ‘a’ is the operand, then pre-decrement operator applied on ‘a’ would result in --a
If a = 5, Evaluate p = 5+ --a;
p=5+4=9
b) Unary post-decrement operator - This is applied after the operand and works on
the principle of “Change the Value after the operation is performed”.
Example: If ‘a’ is the operand, then post-decrement operator applied on ‘a’ would result in
a--
If a = 5, Evaluate p = 5+ a--;
p = 5 + 5 = 10, Value of ‘a’ will be stored as 4 after the operation is performed.
TYPES OF OPERATORS
Binary Arithmetic Operators
These operators are used to perform arithmetic operations such as addition, subtraction,
multiplication and division.
The following are the binary arithmetic operators along with examples
Operation Operator Syntax Result Example : If A = 10, B = 2
Example/Result
Operator Meaning Syntax
If A = 10, B =6;
< Less than A<B False
> Greater than A>B True
<= Less than or equal to A<=B False
>= Greater than or equal to A >= B True
== Equal to A == B False
!= Not Equal to A != B True
TYPES OF OPERATORS
LOGICAL OPERATORS
Operators that combine two or more relational expressions and return a Boolean value based on the Boolean
result of the relational expressions involved are called as Logical Operators.
The three types of Logical Operators are:
1) Logical AND: This operator returns a True value if and only if both the expressions result in a True value.
Symbol Used: &&
Example:
If a = 7, b = 5,c=8 then the expression (a > b && b >c), will result in False value.
The expression (6==6 && 3 >0) will result in True since both conditional Expressions are true.
2) Logical OR: This operator returns a True value if any one of the expressions results in a True value.
Symbol used: ||
Example:
If a = 4, then the expression (a < 0 || a <12), will return a true value since the second expression ‘a<12’ returns
a true value.
The expression (5>4 || 8 <12) will result in true since both expressions are true individually.
3) Logical NOT: This operator reverts the outcome of an expression. Can be applied on one or more relational
expressions.
Symbol used: !
Example: !(8 > 3) , Results in False since the value of the expression (8>3) is true.
TYPES OF OPERATORS
BITWISE OPERATORS
Operators which perform operations on a bit level of the operands are called Bitwise
operators. These operators are binary operators and use byte, short, int and long type
operands. Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ Bitwise complement
<< Left Shift
>> Right Shift
>>> Zero fill right shift
<< = Left shift assignment
>> = Right shift assignment
>>> = Zero fill right shift assignment
TYPES OF OPERATORS
BITWISE OPERATORS
Bitwise AND: This operator results in a high (1 bit) if both operands are high, otherwise a
low value(0 bit). A
0
B
0
A&B
0
Truth Table: 0
1
1
0
0
0
1 1 1
Bitwise OR: This operator results in a high (1 bit) if any one of the operands is high,
otherwise a low value(0 bit).
Truth Table: A B A|B
0 0 0
0 1 1
1 0 1
1 1 1
TYPES OF OPERATORS
BITWISE OPERATORS
Bitwise NOT: This operator results in high(1 bit) if the operand is low and low(0 bit) if
operand is high. It is a Unary operator.
A ~A
Truth Table: 0 1
1 0
Bitwise XOR: This operator results in low(0 bit) if both operands are same, otherwise a
high(1 bit) value.
A B A^B
Truth Table:
0 0 0
0 1 1
1 0 1
1 1 0
TYPES OF OPERATORS
BITWISE OPERATORS
Example:
If a =12, b = 10, evaluate the following:
1. a & b
Solution : Binary value of 12 : 1100 Therefore, the decimal equivalent of the
Binary value of 10 : 1010 Bitwise AND operation result is : 8
Bitwise And Operation : 1000
2. a | b
Solution : Binary value of 12 : 1100 Therefore, the decimal equivalent of the
Binary value of 10 : 1010 Bitwise OR operation result is : 14
Bitwise And Operation : 1110
TYPES OF OPERATORS
BITWISE OPERATORS
Example:
If a =12, b = 10, evaluate the following:
3. a XOR b
Solution : Binary value of 12 : 1100 Therefore, the decimal equivalent of the
Binary value of 10 : 1010 Bitwise XOR operation result is : 6
Bitwise And Operation : 0110
4. a << 2 = 48
Here, the binary value of the operand ‘a’ is shifted to the left by 2 bits.
i.e. Binary value of 12 : 1100 , After shifting the bits to the left by 2 bits : 110000 which is 48 in decimal
5. 12 >> 2 =3
Here, the binary value of the 12 is shifted to the right by 2 bits.
i.e. Binary value of 12 : 1100 --- 0011, After shifting the bits to the right by 2 bits : 0011 which is 3 in decimal
0011
8421- place value
6. 14 >>> 2 =3
Here, the binary value of the 14 is shifted to the right by 3 bits.
i.e. Binary value of 14 : 1110, After shifting the bits to the right by 2 bits : 0011 which is 3 in decimal, the first two digits are
replaced with a binary ‘0’ after shifting two bits to the right.
TYPES OF OPERATORS
TERNARY OPERATOR
Operators which operate on three operands are called Ternary operators. They are also called conditional
assignment statement because the value assigned to a variable depends upon a logical expression.
Example:
1) For a class ‘Student’ , an object can be created using new operator as follows:
Student obj = new Student();
Where, ‘obj’ is the object of type class ‘Student’.
2) int arr [ ] = new int[10]; ----- creates an array ‘arr’ and allocates memory to store 10 integer values
and points to the first location in the array
. (DOT) OPERATOR
The . (dot) operator is used to access individual data members of an object, once the memory
is allocated to an object using the new operator.
Example: Consider a class ‘Smartphone’ , which has common characteristics and behaviour
as below
Common Characteristic Common Behaviour
Operating System games( )
Model Play_music( )
Colour makeCall ( )
RAM ReceiveCall ( )
Battery
. (DOT) OPERATOR
For the class Smartphones, multiple instances(objects of type class Smartphone) can be
created to represent specific smartphones individually.
Use the new operator to create the instances, as below:
Smartphones iPhone = new Smartphones( );
Smartphones Samsung = new Smartphones( );
Smartphones OnePlus = new Smartphones( );
For the objects created above(i.e. iPhone, Samsung, OnePlus), separate memory is allocated to
hold data members individually.
Each object has been allocated separate memory area and has individual space to hold
data members.
OS Games( )
OS Games( ) Play_music( )
Play_music( ) Model
Model makeCalls( )
makeCalls( ) Colour receiveCalls( )
Colour receiveCalls(
) RAM
RAM
Battery
Battery Capacity(mAH) Capacity(mAH)
Samsung
iPhone
Using the . (dot) operator, the individual members of each object
OS Games( ) can be accessed.
Play_music( ) For example, if the attributes of object iPhone has to be accessed to
makeCalls( ) store values or display the values , use the syntax given below:
receiveCalls( ) <objectname>. <membername> = value; ---- to store values in
Model the corresponding member
Colour <objectname>.<membername> --to display the value stored at
the corresponding member of the object.
RAM
OnePlus
Battery Capacity(mAH) Likewise, a function of the object can be accessed as follows:
<objectname>.<functionname>( );
OPERATOR PRECEDENCE
In evaluating expressions, precedence of operators with BEDMAS (Brackets, Exponents, Division, Multiplication,
Addition, Subtraction) system of Hierarchy is followed.
Operators Order of operation (Precedence)
( ), [ ] 1
++, --, ~ 2
*, /, % 3
+, - 4
>>, >>>, << 5
>, >=, <, <= 6
==, != 7
& 8
^ 9
! 10
&& 11
|| 12
?: 13
= 14
NOTE:
Any operator having the same precedence as another operator, then the operator which appears first from
LHS of an expression will be operated first.
EXPRESSIONS IN JAVA
Combination of variables, constants and operators are called Expressions.
Based on the type of operators involved they are classified into different types as:
1. Arithmetic Expressions – Expressions that use arithmetic operators are called
Arithmetic Expressions. Example : C = A+B+D;
Pure Arithmetic Expressions: An arithmetic expression that contains same type of variables or
values to yield a result is called a Pure Arithmetic expression.
Mixed Arithmetic Expression: An arithmetic expression that contains different types of variables or
values to yield a result is called a Mixed Arithmetic expression.
Example: int a, b;
float x;
x = (float) (a+b);
In the above expression, the value of the expression (a+b) will be of type ‘int’ by implicit conversion. However, since the
datatype (float) is mentioned by the user, the value of the expression is explicitly converted to a float value and stored in
variable ‘x’.
Note: Type casting/Explicit type conversion can also be applied to convert the type from a higher type to lower type.
Example: double x, y;
int c = (int) (x+y);
In the above example, the result will be of ‘double’ type implicitly, but due to an explicit type conversion, the result is converted
to a lower type ‘int’.
EXPLICIT TYPE CONVERSION
NOTE:
Refer to Page 86 – for explicit type conversion of different types of values
DISADVANTAGE OF TYPE CONVERSION
Conversion Potential Problems Encountered
Bigger floating-point type to smaller floating-point Loss of Precision. Original value may be out of
type(double to float) range for target type, in which case the result is
undefined.
Floating-point type to int Loss of fractional part. Original value may be out of
range for the target type, in which case the result is
undefined
Bigger integer type to smaller integer type (ex: Original value may be out of range for target type,
long to short) thereby resulting in loss of information.
MATHEMATICAL EXPRESSION IN JAVA
Mathematical forms of expressions must be converted into java expressions in
a java program.
Example:
Mathematical Java Expression
Expression
abc a*b*c
ab + bc + cd (a*b) + ( b * c)+ ( c*d)
prt (p*r*t) /100
100
A2 + B2 (A *A ) + ( B *B)
SHORTHAND NOTATION OF EXPRESSIONS
An expression in Java can be written in shorthand form.
Example: