Вы находитесь на странице: 1из 19

OPERATORS IN

JAVA
Presented By : Yash Arora and Zehra zaidi
B.Tech 2nd year
Computer science &Engg

Operators:
An operators is symbol that are used in

programs to compute values, and test logical


expressions.
An operator performs an action of variables,

contants and variables


Associated with an operator are one or two

operands that receive the action of the


operator

Types Of Operators:
Java operator can be classified into :
a. Arithmetical operators
b. Assignment operators
c. Comparison operators
d. Unary operators
e. Shift operators
f. Bitwise operators
g. Logical operators
h. Conditional operators
i. Instance of and member selection operator
j. New operator

Arithmetical Operator
It is used to do arithmetical operations like additions,

subtractions, multiplications, division and modulo division.


+
Addition
A+B
Add A and B
+

Operators
*
/
%

Addition

Description
Subtraction
Multiplication
Division
Modulo division

A+B

Example
A-B
A*B
A/B
A%B

Add A and B
Meaning
Subtract A and B
Multiply A and B
Divide A and B
Calculate the remaindr
of an integer
division of A and B

public class Example{


public static void main (String args[]) {
int j, k, p, q, r, s, t;
j=5;
k=2;
p=j+k;
q=j-k;
r=j*k;
s=j/k;
t=j%k;
System.out.println(p = + p);
System.out.println(q = +q);
System.out.println(r = +r);
System.out.println(s = +s);
System.out.println(t = +t);
}
}

>Java Example
p=7;
q=3;
r=10;
s=2;
t=1;

Assignment Operator
Operato
r

+=
-=
*=
/=

%=
%B

Description

Exampl Meaning
e
A+=B
A=A+B

Add the right operand and


assign the result to the left operand
Subtract the right operand and assign
the result to the left operand
Multiply the right operand and assign
the result to the left operand
Divide the right operand and assign
the result to the left operand
Divide the left operand by right and
and assign the remainder to left
operand

A-=B

A=A-B

A*=B

A=A*B

A/=B

A%=B

A=A/B

A=A

class OpEquals{
public static void main(String args[]){
int a = 1;
int b = 2;
int c = 3;
a+ = 5;
b* = 4;
c+ = a*b;
c% = 6;
System.out.println(a=+a);
System.out.println(b=+b);
System.out.println(c=+c);
}
}
java OpEquals
a=6
b=8
c=3

Comparison Operator
These operators are evaluating true or false
It is mostly used in conditional statement and

looping nature
Operator

<
<=
>
>=
==
!=

Meaning

Example

Less than
Less than or equal to
Grater than
Grater than or equals to
Equal to
Not Equal to

A<B
A<=B
A>B
A>=B
A==B
A! = B

Unary Operator
Increment(++) & Decrement(--) Operator

++ increment by 1 -- decrement by 1
Example:
count ++;// count= count+1;
count--;// count=count-1;
Postfix version(var++, var--) : use value of var
in expressions, then increment and decrement
Prefix version(++var, --var) : increment or
decrement var then use value in expressions

class IncDec {
public static void main(String args[]) {
int a = 1; int b = 2;
int c;
int d;
c = ++b;
d = a++;
c++;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
}
}

java IncDec
a=2
b=3
c=4
d=1

Shift Operator
Its allow to perform bit manipulation on data.
Java support three types of shift operators
1. Left shift <<
2. Right shift >>
3. Right shift with zero fill >>> (or) unsigned right shift

Operator General Form


>>
Op1>>Op2
distance of OP2
<<
Op1<<Op2
distance of Op2
>>>
Op1>>>Op2
distance of Op2

Operation
Shifts bits at Op1 right by

Shifts bits at Op1 left by


Shifts bits at Op1 right by

Bitwise Operator
Java supports four types of bitwise operators they are:
1. And(&)
2. OR( | )
3. XOR (^)
4. NOT (~)

0
A
1
0
1

B0
0
1
1

A|B 0
1
1
1

A&B 0

A^B 0
0
0
1

~A
1
1
0

1
0
1
0

Logical Operator

Two logical operators are in java


1. AND(&&)
2. OR ( || )
Operand
1

True
True
False
then
False

Operand 2
True
False
True
False

Operand1

Operand 2

True
True
False
False
false

True
False
True
False

Operand 1
&&
True
Operand
2
False
False

False

example
if ( X>2 && Y<10) The result
if both conditions are true;
if one of the result is false
the result is false

Operand 1
|| operand2
True
True
True
False

Example
If( X>2 || Y<10) The resu
is true if eithercondition
are true; if both results are
false, then the result is

Conditional operator
Its a decision operator . Its also known as

ternary operator.
Its general form is

Any expression that evaluates


to a Test condition

Test condition ? Expression 1 : Expression


2;
If true this expression is
evaluated and becomes the
value entire expression.

If false this expression is


evaluated and becomes the
value entire expression.

Example:
1. result = marks >=30? P : F;
if marks=58
result = P;
2. rate = time>5? 1.5 : 1.2;
if time = 3
rate = 1.2;

Instanceof operator
Its a reserved word in java. The return type is

Boolean
General form:
Object instanceof Class;
If object is of specified type then instanceof
return true otherwise return false
It is also known as Runtime Operator
Example:
rose insatnceof flower;
Is true, if the object rose belongs to the class
flower;
otherwise it is false

New () Operator
The New operator is used when we allocate

memory to an object
Its have two ways:
Declare the object then allocate memory
Classname objectname;// declaration
Objectname = new Classname();//memory
allocation
Declare the object as well as the allocation of
memory
Classname objectname = new classname();

Operator Precedence
Unary
Arithmetic
Shift
Comparison
Logical Bit
Boolean
Ternary
Assignment

+ - ++ -- ! ~ ()
* / %
+ << >> >>>
> < >= <= instanceof
== !=
& | ^
&& ||
?:
= (and += etc.)

Вам также может понравиться