9 cm604.10

33
1 Various types of operators

description

 

Transcript of 9 cm604.10

Page 1: 9 cm604.10

1

Various types of operators

Page 2: 9 cm604.10

2

Objectives

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

• Various operators in Java

Page 3: 9 cm604.10

3

Recap

• What is an array

• What is one dimensional array

• What is two dimensional array

Page 4: 9 cm604.10

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

Page 5: 9 cm604.10

5

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

OperatorsContd..

Page 6: 9 cm604.10

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

Page 7: 9 cm604.10

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..

Page 8: 9 cm604.10

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

Page 9: 9 cm604.10

9

• The syntax for relational operator is

Expression1 <relational operator> Expession2

• Here, Expression1 and Expression2 are arithmetic

expressions

Relational OperatorsContd..

Page 10: 9 cm604.10

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

Page 11: 9 cm604.10

11

Logical Operators

• Java has three logical operators

&& - Logical AND

| | - Logical OR

! - Logical NOT

Page 12: 9 cm604.10

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..

Page 13: 9 cm604.10

13

Assignment Operator

• These are used to assign the value of an

expression to a variable

• The assignment operator is ‘= ‘

Page 14: 9 cm604.10

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..

Page 15: 9 cm604.10

15

• The above form is equivalent to

V = V op value;

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

Contd..

Assignment Operator

Page 16: 9 cm604.10

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

Page 17: 9 cm604.10

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

Page 18: 9 cm604.10

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..

Page 19: 9 cm604.10

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

Page 20: 9 cm604.10

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 ;

Page 21: 9 cm604.10

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

Page 22: 9 cm604.10

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

Page 23: 9 cm604.10

23

Bitwise Operators

• Java has a distinction of supporting special

operators known as ‘Bitwise Operators’

• These manipulate data at values of bit level

Page 24: 9 cm604.10

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

Page 25: 9 cm604.10

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

Page 26: 9 cm604.10

26

Special Operators

• Java supports some special operators

• Such as ‘ instanceOf ‘ operator

• And ‘Dot’ operator or member selection operators

Page 27: 9 cm604.10

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

Page 28: 9 cm604.10

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

Page 29: 9 cm604.10

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

Page 30: 9 cm604.10

30

Summary

• Different types of Operators

Page 31: 9 cm604.10

31

Quiz

1.An Operator Operates______

a) Variables

b) Operators

c) Operands

d) All the Above

Page 32: 9 cm604.10

32

Frequently Asked Questions

• List any Three Logical operator

• List any Four Bitwise Operators

• Explain various operators available in Java

Page 33: 9 cm604.10

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