[go: up one dir, main page]

0% found this document useful (0 votes)
12 views11 pages

#20 Operators

The document provides an overview of operators in Java, defining key concepts such as operators, operands, and expressions. It categorizes operators into arithmetic, relational, logical, unary, assignment, bitwise, and ternary operators, explaining their functions and providing examples. Additionally, it discusses operator precedence and associativity in evaluating expressions.

Uploaded by

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

#20 Operators

The document provides an overview of operators in Java, defining key concepts such as operators, operands, and expressions. It categorizes operators into arithmetic, relational, logical, unary, assignment, bitwise, and ternary operators, explaining their functions and providing examples. Additionally, it discusses operator precedence and associativity in evaluating expressions.

Uploaded by

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

OPERATORS IN JAVA

What
* are
operators ?
This indicates what action to
perform
-

What is
*
operand ?
This indicates the items which action has to
apply
-
on on .

* What is expression ?
It consists low
operand and
of operators
-
more 0 or more .

Eg : s + 3

-
S$3 are
operands
-

+ is operator
-

5 +3 as a whole is Kla expression

=>
Categories
Of
7
Operators
of
here
I are
categories operators

17 Arithmetic
Operators
·

/(Division)
· -

/Subtraction)
·
+ (Addition

1
%
·
Modulus)

·
*
/Multiplication (

We
already
have
information from mathematics
of
how arithmetic
work
operators .

For
agi-

2) Relational Operators
-
These compares two
operand relation and return true or
false
.

&
== (Equals to ⑧ < (Less than

1
·
= (Not equalstr) · >=
/Greater than or
equals to
& -
(Greater tan) - I Less than or equals to

For
Eg : -
)
3 .

Logical Operators
These combines conditions
two or more and return true ar
false
-

SS/Logical AND) Returns only if all conditions


-

true are true


.

· 11
(Logical OR) Returns true if
-

condition is atleast one true

For Egi-

It
Its seen
*
if one condition is
false ,
will not even evaluate
further
conditions .

4 ) .

Unary Operators
These require only single operand
-

·
++ /Increment)

·
--
(Decrement (
Minus)
(Unary
-
·

+
(Unary Plus)
·

!
·

(Logical NOT)

k-- be used
before after operand.
* + + can or

So
Operand ++ >
Postfix Increment + +
Operand =>
Prefix Increment
E

Operand --
=>
Postfix Decrement --

Operand => Prefix Decrement


-

Postfix will return whatever value is


first
$ then increments I decrements

the value

I decrement the value I


Prefix will
first increment then returns the
-

value .

Logical NOT operator will reverse the value ie


if current value is true
,

itdo it'll
it'll
change false Bif the current value is
false ,
change the

value to true
.

Univy + and-makes the value


positive or
negative .
-

For
Egi-
5.
)
Assignment Operators
-

These are used to


assign new value to the variable .

G
- ·
*

·
+ I · I =

6 - I · % =

Assignment Operator assigns a value (an


right side
of operator to

avariable (on
left side of the
operator

For
Eg : -

6) Bitwise Operator
These works bits i < 1 and 0 and are
very fast
.
-

on .

·
&(Bitwise AND)
·
1( Bitwise OR)

·
n) Bitwise XOR)

~
· (Bitwise NOT

(It under tool


can come
Unary
* Bitwise AND (B) * Bitwise OR 11) * Bitwise X OR (N)

a b a b a b

O 0 = 0 g O
=> O O O => s

01 = O O I => I G I
>
-

-
I

I 0 = 0 I G => I ( 8 => I

I 1 - 1 I > I I
-

I - I => o

-
tor
Eg : -

* How does bitwise NOT work


?
It
basically reverses the bit
-

So 0 = 1

1 = 6

Now how does it works on numbers


.

Let's
compute
-y
(NoTg4)
its we know in Java numbers are
signed i . e Most
Significant Bit

(MSB) tells the


sign of
the number
So 1
binary of
will be

& do 2
20

it is
(MSB) Since MsBisO so
positive .
-

Now will all the bits


binary of will be
Not
wy
reverse so
,

3
22
I
O
2 2 2

( ) I
-
-

(MSB3)
Since MSBistie result is-he value
of negative
,
so the MSB will be

decimal
Now
converting it to
gives
us

-
8 + 0 + 2 + 1

i .
e
-

So 0100 ---
> -0100 = 101

We calculate it (N + 1)
directly using formula
can the -

.
So in o u r case

it'll be (n + 1) ie .
5
=
-

is
confirm if-5 have
* How can we 101

To 2
get
5 we know that we to
find its
Complement
-
-

So
for 4 ie 0101

1st Complement = 1010

2 Complement = 1st complement + 1

.
i e 1010 + 1 => 1011

So bitwise bitwise complement of any mis-(n 1)


NOT
, integer +
7) Bitwise
Shift Operators
These bits
> are used to
shift the of
a number
left or
right
.
-

·
/Signed Left Shift
· (Signed Right Shift
·

(Unsigned Right Stiff


-
There is unsigned left shift
no as and are .
equal

*: Its
signed right shift
It
fills the most significant bit with the
sign of
the number
-

Eg : -
1) 11000116's 7 will be

11100011

em
shifted

right
MSB added as same
sign original
of
the number

2) 01000110's 77 will be

· 010 0 0 11
-

shifted right
MSB added as same
sign of
the
original number

It's
* >> :
unsigned right shift
It
fills with
the MSB the O :
-

/1000110's
Eg : 1) > will be

O 1 10 0 0 11
-

shifted right
MSB
filled with o

el 0 1000110's will be

00100011
-

I shifted
MSB
right
added O
as
Demo for born
left right shift :-

For
*
Left Shift LSB will
always filled with 0
.

*
Left Shift once doubles the number

*
RightShift once halves the number

-
don't have value
of
* There is no sense <LL as LSB
any

8)
Ternary Operators
It mimics the condition
if else
-

So the condition it'll


first expression
-

it evaluates execute otherwise


, ,

it'll execute second expression


.

(condition)
? Expression 1 : Expression 2
-

For
Egi-
a)
Type Comparison Operator
It is used to do the check particular object is
type whether
of
-

a
,

certain class .
or not

instance
Of
-

For
Egi-

It'll return true when the child class


perform type check
of object of
* we an

with its class


parent .

=> Operator Precedence :-

have precedence
Associativity If
I operators
: the same then it is evaluated
-

based on its
associativity /Left Ju
Right or
Right to
Left).
High

Priority

Low

* Let's solve an
example expression

inta = 4

Solve a = a + a+ + + ++ a + - -
a + a --

Now replace operands with values

a = 45054
.. a = 4 + 4 + 6 * 5 + 5

a = n + y + 30 + 5

a = 39

You might also like