Presented by:
SUJOY KUMAR BASU
AP, Dept. of Computer Application
AEC, Asansol
Content
2
Operators in J ava
Arithmetic Operator
Relational Operator
Logical Operator
Increment & Decrement Operator
Assignment Operator
Operators in J a v a
3
An expression consists of variables & constants
separated by operators. J ava language uses many types
of operators as listed below.
Arithmetic Operator
Relational Operator
Logical Operator
Increment or Decrement Operator
Assignment Operator
Conditional Operator
Bitwise Operator
Instance of Operator
new Operator
Arithmetic Operator
4
Following table shows all the arithmetic operators supported by
J ava language. Assume variable A is 20 and B is 10, then:
Operator Description Example
+ Add two operands A+B = 30
- Subtract 2 nd operand from 1st A-B = 10
* Multiply both operand A*B = 200
/ Divides 1 st operand by 2 nd A/B = 2
operand
% Find the remainder A%B = 0
Relational Operator
5
It is used to compare the values of operand to produce a
logical value. A logical valu e is eith er true or false.
Following table shows all the relational operators
supported by J ava language.
Operator Meaning Example Result
< Less Than 10<5 false
<= Less Than or Equal to 5<=10 true
> Grater Than 10>5 true
>= Grater Than or Equal to 5>=10 false
== Equal to 5==10 false
!= Not equal to 5!=10 true
Logical Operator
6
Logical operators are used to connect more relational
operations to form a complex expression called logical
expression. A value obtained by evaluating a logical
expression is always logical, means either true or false.
J ava has the following three logical operator.
Operator Meaning Example Result
&& Logical And (5<2)&&(5>3) false
|| Logical Or (5<2)||(5>3) true
! Logical Not !(5<2) true
Increment & Decrement Operator
7
The increment operator (++) is used to increase the
value of the operands by 1 where decrement operator
(--) is used to reduce the value of the operands by 1.
Both are unary operators & take the following forms:
++a or a++ [means a=a+1]
--a or a-- [means a=a-1]
Example:
int x=5, y=10;
x++; / / means x=6
y--; / / means y=9
Increment & Decrement Operator (CONTD.)
8
++a and a++ means the same thing when they form
statements independently & they behave differently
when they are used in expression or the R . H . S . of a n
assignment statement.
Consider the following example
int x=10,y,z;
y=++x; / / pre-increment
printf (“X=%d Y=%d”, x,y); //X=11 & Y=11
z=x++; / / post-increment
printf (“X=%d Z=%d”, x,z); //X=12 & Z=11
Increment & Decrement Operator (CONTD.)
9
Similarly --a and a-- means the same thing when they form
statements independently & they behave differently when they
are used in expression or the R . H . S . of an assignment statement.
Consider the following example
int p=50,q,r;
q=--p; / / pre-decrement
printf (“P=%d Q=%d”, p,q); //P=49 & Q=49
r=p--; / / post-decrement
printf (“P=%d R=%d”, p,r); //P=48 & R=49
Assignment Operator
10
Assignment operators are used to assign the result of an
expression to a variable. We have seen the usual assignment
operator ‘=‘.
J ava has a set of ‘shorthand’ assignment operators of the
following form.
v op= exp;
Where ‘v’ is a variable, ‘exp’ is an expression and ‘op’ is a J ava
binary arithmetic operator.
The op= is known as the shorthand assignment operator.
The shorthand assignment statement
v op= exp;
is equivalent to
v = v op (exp);
with v evaluated only once.
Assignment Operator (CONTD.)
11
Example: Consider the following example
x += y*5;
This is same as the following statement
x = x + (y*5);
Some of the commonly used shorthand assignm ents
operators are illustrated in the following table.
Operator Example Equivalent
Expression
+= a+=10 a=a+10
-= a-=10 a=a-10
*= a*=10 a=a*10
/= a/=10 a=a/10
%= a%=10 a=a%10
Thank You.