You are on page 1of 10

Presented by:

SUJOY KUMAR BASU


AP, Dept. of Computer Application
AEC, Asansol
Contents
2

 Conditional Operator

 Bitwise Operator

 Instanceof Operator

 New Operator
Conditional Operator
3

A ternary operator pair “? :” is available in J a va .


Conditional operator is used to check a condition & select a
value depending on the value of the condition, Normally the
selected value will be assigned to a variable which has the
following form.

variable = (condition) ? value1 : value2;

Example:
int a=10,b=20;
max = (a>b) ? a : b;
Conditional Operator (CONTD.)
4

E xample: Find the min & max number of given 3


numbers using ternary operator.
Soln:
int a, b, c, max, min;
a=100; b=50; c=70;

max = (a>b) ? ((a>c) ? a : c) : ((b>c) ? b : c);

min = (a<b) ? ((a<c) ? a : c) : ((b<c) ? b : c);


Bitwise Operator
5

Bitwise operators are used to perform operations at binary digit level.


These operators are not commonly used & are used only in special
applications where optimized use of storage is required. Following are
the bitwise operators used in J a va language.

Operator Meaning
<< Shifts the bits to left
>> Shifts the bits to right
>>> Right shift with zero fill or
unsigned right shift
& Bitwise AND
| Bitwise O R
~ Bitwise inversion (1’s
complement)
^ Bitwise Exclusive O R
Left shift (<<)
6

Let a=13; its binary equivalent is 00001101.


2|13 Remainder
2|6 1
2|3 0
2|1 1
0 1
Consider a<<2; which shifts 2 bits to the left. That is, 2 zeros are
inserted at the right to shift 2 bits at the left to move out.
0 0 0 0 1 1 0 1

0 0 1 1 0 1 0 0 0

So, the result obtained in this operation is 00110100=(52) 10 .


Right shift (>>)
7

Let a=15; its binary equivalent is 00001111.


2| 1 5 Remainder
2|7 1
2|3 1
2|1 1
0 1
Consider a>>2; which shifts 2 bits to the right. That is, 2 zeros are inserted
at the left to shift 2 bits at the right to move out. The leftmost bit depends on
the sign of initial number

0 0 0 0 1 1 1 1

0 0 0 0 0 0 0 1 1

So , the result obtained in this operation is 00000011=(3)10 .


Instance Of Operator
8

The “instanceof ” is an object reference operator


& returns true if the object on the left hand side
is an instance of the class given on the right
hand side. This operator allows u s to determine
whether the object belongs to a particular class
or not.
Example: person instanceof student;
Is true if the object ‘person’ belongs to the class
’student’; otherwise it is false
new Operator
9

To allocate memory to an object; we must use the new operator.

 D eclare the object as well as the allocation of memory in a


single step.

Syntax: class_name object_name=new class_name();


E x ample: Student s = n e w Student();
Thank You.

You might also like