9 cm604.10

Post on 31-Oct-2014

234 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

1

Various types of operators

2

Objectives

On completion of this period, you would be able to know:

• Various operators in Java

3

Recap

• What is an array

• What is one dimensional array

• What is two dimensional array

4

Operators

• An operator performs an operation on data items

(also known as operands)

• Java operators can be classified as follows

• Arithmetic operators

• Relational operators

• Logical operators

5

• Assignment operators• Increment/Decrement operators• Conditional operators• Bit-wise operators• Special operators

OperatorsContd..

6

Arithmetic Operators

• Java provides all the basic arithmetic operators

like + , - , * , / , %

• These can operate on any built in numeric data

types like any other languages

7

• We can not use these operators on boolean data

type

eg. : These are used as

a+b , a-b , a*b , a/b , a% b

• Here a, b are variables or constants known as

operands

Arithmetic OperatorsContd..

8

Relational Operators

• These operators are used for compare two

quantities

• Java supports the following relational operators

• < , <= , > , >= , = = , !=

• The value of the relational expression is either true

or false

9

• The syntax for relational operator is

Expression1 <relational operator> Expession2

• Here, Expression1 and Expression2 are arithmetic

expressions

Relational OperatorsContd..

10

• When arithmetic expressions are used , it must be evaluated first and then results compared.

eg. : a<b

a<=b

a>b

a>=b

a= = b

a!=b

Contd..

Relational Operators

11

Logical Operators

• Java has three logical operators

&& - Logical AND

| | - Logical OR

! - Logical NOT

12

• An expression which combines two or more relational expressions

• It is called as logical expression or compound expression

eg. : a >b && c <d• The logical expression given above is true only if

both conditions are true• If either of them are false the expression is false

Logical OperatorsContd..

13

Assignment Operator

• These are used to assign the value of an

expression to a variable

• The assignment operator is ‘= ‘

14

• These operators are used for shorthand assignment in the following form

V op = value;

• Here, V is variable ,op is operator.

eg. : a+ = 9;

Assignment Operator

Contd..

15

• The above form is equivalent to

V = V op value;

eg. : a+ = 1 means a= a+1

Contd..

Assignment Operator

16

• Advantages of shorthand assignment operators are

• What appears on the left hand side need not be

repeated and therefore it becomes easier

• The statement is more easier to read

Contd..

Assignment Operator

17

Increment/Decrement Operators

• Java has two useful operators

• These are increment(++) and decrement(--)

operators.

• The operator ++ adds one to the operand and the

operator -- subtracts one

18

• Both are unary operators and these are in different

forms.

eg. : ++m or m++ ,this is equivalent to m+1

--m or m-- ,this is equivalent to m-1

Increment/Decrement Operators

Contd..

19

• The difference occurs only the operator is a part of another expression

eg. : m = 9, k =++ m ; • In this case the value of k would be 10

eg. : m = 9, k = m++ ;• In this case the value of k would be 9 and m

would be 10

Contd..

Increment/Decrement Operators

20

Conditional Operator

• The character pair ‘?:’ is a ternary operator

available in Java

• This operator is used to construct conditional

expressions of the form

Expression1 ? Expression2 : Expression3 ;

21

• Here, ?: works as follows

• Expression1 is evaluated first if it is true

• Then the Expression2 is evaluated and becomes

the value of the conditional expression

Contd..

Conditional Operator

22

• If Expression1 is false, Expression3 is evaluated

• And its value becomes the value of the conditional

expression

eg. : x = (a > b ) ? a : b;

Contd..

Conditional Operator

23

Bitwise Operators

• Java has a distinction of supporting special

operators known as ‘Bitwise Operators’

• These manipulate data at values of bit level

24

• These operators are used for testing the bits or

shifting them to the right or left

• Bitwise operators may not be applied to float or

double

Contd..

Bitwise Operators

25

• The bitwise operators are

& Bitwise AND

| Bitwise OR

^ Bitwise exclusive OR

~ Ones complement

<< Shift left

>> Shift right

>>> Shift right with zero fill

Contd..

Bitwise Operators

26

Special Operators

• Java supports some special operators

• Such as ‘ instanceOf ‘ operator

• And ‘Dot’ operator or member selection operators

27

instanceOf Operator

• The instanceOf operator is an object reference operator

• It returns true if the object on the left hand side is an instance of the class given on the right hand side

28

• This operator allows us to determine the object

belongs to a particular class or not

eg. : s instanceof Student;

• The above statement is true if the object belongs

to the class Student

• Otherwise it is false

Contd..

I

instanceOf Operator

29

Dot Operator

• The Dot operator is used to access the instance of variables and methods of a class

eg. : a.dispaly();

• It is also used to access classes and sub packages

from packages

30

Summary

• Different types of Operators

31

Quiz

1.An Operator Operates______

a) Variables

b) Operators

c) Operands

d) All the Above

32

Frequently Asked Questions

• List any Three Logical operator

• List any Four Bitwise Operators

• Explain various operators available in Java

swingsStrutsjdbchibernatehomejava previous question papers OCT/NOV-2012 QUESTION PAPERApril / May 2012 c-09October/ November-2011 c-09April/ May 2011 c-09April/ May 2011 c-05

  

Home33

top related