[go: up one dir, main page]

0% found this document useful (0 votes)
17 views32 pages

5 - Operators and Expressions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views32 pages

5 - Operators and Expressions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

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

Addition + A+B Returns the sum A + B = 12


Subtraction A–B Returns the A–B=8
-
difference
Multiplication * A*B Returns the product A * B = 20
Division A/B Returns the A/B=5
/ quotient( Integer
part)
Modulus A%B Returns the A%B=0
%
Remainder
TYPES OF OPERATORS
RELATIONAL OPERATORS
 Operators that compare and define a relationship between two values or entities are called
Relational Operators. They generally result in a Boolean value i.e. a True or a false value.
 The following are the Relational operators along with examples:

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.

General Syntax: <datatype> variable = (test expression)? Expression1:Expression 2 ;


Where, variable – variable to hold the final result.
(test expression) – is a conditional expression which results in either results in a true or false value.
Expression 1 – is the value returned and stored in ‘variable’ if the test expression results in True.
Expression 2 – is the value returned and stored in ‘variable’ if the test expression results in False.
Example :
If a =5, b = 3;
Max = (a>b) ? a : b;
The value stored in variable ‘Max’ will be 5, since the expression (a>b) results in True and stores the value of ‘a’.
Min = (b>a) ? a: b;
The value store in variable ‘Min’ will be 3, since the expression (b >a) results in False and stores the value of ‘b’.
ASSIGNMENT OPERATOR
ASSIGNMENT OPERATOR:
The symbol ‘=’ is called an Assignment Operator since it is used to store
(assign) values in variables.
General Syntax:
<variablename> = <value/Expression>;
Example : int a = 5;
a = 5;
NEW OPERATOR
The new operator is used to create a class object or an array. It allocates memory (for storing values of
an object or an array) and returns the reference(i.e. address location) of the memory at run-time.
Syntax to create an object :
Classname object_name = new Classname();

Syntax to create an array:


<data type> <arr_name> [ ] = new <data type>[size];

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.

2. Relational Expression – Expressions that use relational operators are called


Relational Expression. Example : a < b
3. Logical Expression – Two or more relational expressions that involve Logical
operators are called Logical expressions. Example : [(a>b) && (a>c)] if a = 5, b = 3, c = 7
NOTE: If an expression is assigned to a variable then it is known as a statement
TYPE CONVERSION
Converting values of different types of a mixed Arithmetic expression into a
value of one type is called as Type Conversion.

There are two types of Type Conversion:


1. Implicit Type conversion
2. Explicit Type Conversion –Type casting
IMPLICIT TYPE CONVERSION
In this technique, the compiler automatically converts the result of a mixed
expression, into a higher data type value, without user intervention.
Example: int a;
long b, c;
c = a + b;
(long) (int) (long)

(result – long type)


IMPLICIT TYPE CONVERSION RULES
Implicit Type conversion converts all operand to the type of largest operand.
The following rules are followed
If either operand is of type double, the other is converted to double.
If either operand is of type float, the other is converted to float.
If either operand is of type long, the other is converted to long.
Otherwise, both operands are converted to type int.
NOTE: Once, the above rules are applied and each of the pair of the operands
are of same type , the resultant operand will be of the same type as that of
both operands used in the operation.
DISADVANTAGE OF IMPLICIT TYPE CONVERSION
Coercion (Implicit Type Conversion) decreases the type error detection
ability of the compiler.
EXPLICIT TYPE CONVERSION
In this technique, the user explicitly converts the data type of a value into another type. It is also called as Type
Casting.

General Syntax: <data type> <variable or 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:

Expression Shorthand form


a= a+b a += b --- a = a+ b
d = d/2 d /= 2
x=x%2 x %= 2
A=A*B A *= B

You might also like