2. Java Operators
The operators in Java are like the following :
1. Arithme*c
operators
.
2. Rela*onal
operators.
3. Logical
operators.
4. Assignment
operators.
3. Arithmetic Operators
Operator
Descrip-on
Example
+
Addi-on
–
Adds
values
on
either
side
of
the
operator
A + B will give 30
-‐
Subtrac-on
–
Subtracts
right
hand
from
le?
hand
A - B will give -10
*
Mul-plica-on
-‐
Mul-plies
values
on
either
side
of
the
operator
A * B will give 200
/
Division
-‐
Divides
le?
hand
operand
by
right
hand
operand
B / A will give 2
%
Modulus
-‐
Divides
le?
hand
operand
by
right
hand
operand
and
returns
remainder
B % A will give 0
++
Increment
-‐
Increases
the
value
of
operand
by
1
B++ gives 21
-‐-‐
Decrement
-‐
Decreases
the
value
of
operand
by
1
B-- gives 19
Arithmetic operators are used in mathematical expressions in the same way that they
are used in algebra.
The following table lists the arithmetic operators:
Assume integer variable A holds 10 and variable B holds 20, then:
4. public static void main(String[] args) {
int A , B ;
A=10 ;
B=20 ;
System.out.println ( A + B ) ;
}
010101010101010101010101
30
Addition Operator
A=10 B=20
5. public static void main(String[] args) {
int A , B ;
A=10 ;
B=20 ;
System.out.println ( A - B ) ;
}
010101010101010101010101
-10
Subtraction Operator
A=10 B=20
6. public static void main(String[] args) {
int A , B ;
A=10 ;
B=20 ;
System.out.println ( A * B ) ;
}
010101010101010101010101
200
Multiplication Operators
A=10 B=20
7. public static void main(String[] args) {
int A , B ;
A=10 ;
B=20 ;
System.out.println ( B / A ) ;
}
010101010101010101010101
2
Divition Operator
A=10 B=20
8. public static void main(String[] args) {
int A , B ;
A=10 ;
B=20 ;
System.out.println ( B % A ) ;
}
010101010101010101010101
0
Modulus Operator
A=10 B=20
9. public static void main(String[] args) {
int A , B ;
A=10 ;
B=20 ;
System.out.println ( + + B ) ;
}
010101010101010101010101
Increment Operator
A=10 B=20
10. public static void main(String[] args) {
int A , B ;
A=10 ;
B=20 ;
System.out.println ( + + B ) ;
}
010101010101010101010101
Increment Operator
A=10 B=21
11. public static void main(String[] args) {
int A , B ;
A=10 ;
B=20 ;
System.out.println ( + + B ) ;
}
010101010101010101010101
Increment Operator
A=10 B=21
21
12. public static void main(String[] args) {
int A , B ;
A=10 ;
B=20 ;
System.out.println (B + +) ;
}
010101010101010101010101
20
Increment Operator
A=10 B=20
13. public static void main(String[] args) {
int A , B ;
A=10 ;
B=20 ;
System.out.println (B + +) ;
}
010101010101010101010101
20
Increment Operator
A=10 B=21
14. public static void main(String[] args) {
int A , B ;
A=10 ;
B=20 ;
System.out.println ( - - B) ;
}
010101010101010101010101
Decrement Operator
A=10 B=20
15. public static void main(String[] args) {
int A , B ;
A=10 ;
B=20 ;
System.out.println ( - - B) ;
}
010101010101010101010101
Decrement Operator
A=10 B=19
16. public static void main(String[] args) {
int A , B ;
A=10 ;
B=20 ;
System.out.println ( - - B) ;
}
010101010101010101010101
Decrement Operator
A=10 B=19
19
17. public static void main(String[] args) {
int A , B ;
A=10 ;
B=20 ;
System.out.println ( B - - ) ;
}
010101010101010101010101
Decrement Operator
A=10 B=20
18. public static void main(String[] args) {
int A , B ;
A=10 ;
B=20 ;
System.out.println ( B - - ) ;
}
010101010101010101010101
20
Decrement Operator
A=10 B=20
19. public static void main(String[] args) {
int A , B ;
A=10 ;
B=20 ;
System.out.println ( B - - ) ;
}
010101010101010101010101
20
Decrement Operator
A=10 B=19
20. Relational Operators
operator
Descrip-on
Example
==
Checks
if
the
values
of
two
operands
are
equal
or
not,
if
yes
then
condi-on
becomes
true.
(equal
)
(A
==
B)
is
not
true.
!=
Checks
if
the
values
of
two
operands
are
equal
or
not,
if
values
are
not
equal
then
condi-on
becomes
true.
(
not
equal
)
(A
!=
B)
is
true.
>
Checks
if
the
value
of
le?
operand
is
greater
than
the
value
of
right
operand,
if
yes
then
condi-on
becomes
true.
(
greater
than)
(A
>
B)
is
not
true.
<
Checks
if
the
value
of
le?
operand
is
less
than
the
value
of
right
operand,
if
yes
then
condi-on
becomes
true.
(
Less
than)
(A
<
B)
is
true.
>=
Checks
if
the
value
of
le?
operand
is
greater
than
or
equal
to
the
value
of
right
operand,
if
yes
then
condi-on
becomes
true.
(A
>=
B)
is
not
true.
<=
Checks
if
the
value
of
le?
operand
is
less
than
or
equal
to
the
value
of
right
operand,
if
yes
then
condi-on
becomes
true.
(A
<=
B)
is
true.
There are following relational operators supported by Java language:
Assume variable A holds 10 and variable B holds 20 , then:
21. public static void main(String[] args) {
int x , y ;
x = 10 ;
y = 20 ;
System.out.println ( x = = y ) ;
}
010101010101010101010101
False
Relational Operators
x = 10 y = 20
22. public static void main(String[] args) {
int x , y ;
x = 10 ;
y = 20 ;
System.out.println ( x ! = y ) ;
}
010101010101010101010101
True
Relational Operators
x = 10 y = 20
23. public static void main(String[] args) {
int x , y ;
x = 10 ;
y = 20 ;
System.out.println ( x > y ) ;
}
010101010101010101010101
False
Relational Operators
x = 10 y = 20
24. public static void main(String[] args) {
int x , y ;
x = 10 ;
y = 20 ;
System.out.println ( x < y ) ;
}
010101010101010101010101
True
Relational Operators
x = 10 y = 20
25. public static void main(String[] args) {
int x , y ;
x = 10 ;
y = 20 ;
System.out.println ( x > = y ) ;
}
010101010101010101010101
False
Relational Operators
x = 10 y = 20
26. public static void main(String[] args) {
int x , y ;
x = 10 ;
y = 20 ;
System.out.println ( x < = y ) ;
}
010101010101010101010101
True
Relational Operators
x = 10 y = 20
27. Logical Operators
Operator
Descrip-on
Example
&&
Called
Logical
AND
operator.
If
both
the
operands
are
non-‐zero,
then
the
condi-on
becomes
true.
(A
&&
B)
is
false.
||
Called
Logical
OR
Operator.
If
any
of
the
two
operands
are
non-‐zero,
then
the
condi-on
becomes
true.
(A
||
B)
is
true.
!
Called
Logical
NOT
Operator.
Use
to
reverses
the
logical
state
of
its
operand.
If
a
condi-on
is
true
then
Logical
NOT
operator
will
make
false.
!(A
&&
B)
is
true.
The following table lists the logical operators:
Assume Boolean variables A holds true and variable B holds false , then:
28. public static void main(String[] args) {
int test = 6 ;
If ( test = = 9 ) {
System.out.println( “ yes ” ) ;
} else {
System.out.println( “ this is else ” ) ;
}
}
010101010101010101010101
this is else
If Statement
test = 6
29. public static void main(String[] args) {
int boy , girl ;
boy = 18 ;
girl = 68 ;
If ( boy > 10 ) {
System.out.println(“you can enter”) ;
}else{
System.out.println(“you are too young”)
}
}
010101010101010101010101
You can enter
Logical Operators
boy = 18 girl = 68
30. public static void main(String[] args) {
int boy , girl ;
boy = 18 ;
girl = 68 ;
If ( boy > 10 && girl < 60 ) {
System.out.println(“you can enter”) ;
}else{
System.out.println(“you are too young”);
}
}
010101010101010101010101
You are too young
Logical Operators
boy = 18 girl = 68
31. public static void main(String[] args) {
int boy , girl ;
boy = 18 ;
girl = 68 ;
If ( boy > 10 | | girl < 60 ) {
System.out.println(“you can enter”) ;
}else{
System.out.println(“you are too young”);
}
}
010101010101010101010101
You can enter
Logical Operators
boy = 18 girl = 68
32. public static void main(String[] args) {
int boy , girl ;
boy = 18 ;
girl = 68 ;
If (!( boy > 10 && girl < 60 )) {
System.out.println(“you can enter”) ;
}else{
System.out.println(“you are too young”);
}
}
010101010101010101010101
you can enter
Logical Operators
boy = 18 girl = 68
33. public static void main(String[] args) {
int age ;
age = 3 ;
Switch( age ) {
case1 :
System.out.println(“you can crawl”) ;
break ;
case2 :
System.out.println(“you can talk”) ;
break ;
case3 :
System.out.println(“you can get in trouble”) ;
break ;
default :
System.out.println(“I don’t know how old are you”)
}
010101010101010101010101
you can get in trouble
Logical Operators
age= 3
34. Assignment Operators
Operator
Descrip-on
Example
=
Simple
assignment
operator,
Assigns
values
from
right
side
operands
to
le?
side
operand
C
=
A
+
B
will
assign
value
of
A
+
B
into
C
+=
Add
AND
assignment
operator,
It
adds
right
operand
to
the
le?
operand
and
assign
the
result
to
le?
operand
C
+=
A
is
equivalent
to
C
=
C
+
A
-‐=
Subtract
AND
assignment
operator,
It
subtracts
right
operand
from
the
le?
operand
and
assign
the
result
to
le?
operand
C
-‐=
A
is
equivalent
to
C
=
C
-‐
A
*=
Mul-ply
AND
assignment
operator,
It
mul-plies
right
operand
with
the
le?
operand
and
assign
the
result
to
le?
operand
C
*=
A
is
equivalent
to
C
=
C
*
A
/=
Divide
AND
assignment
operator,
It
divides
le?
operand
with
the
right
operand
and
assign
the
result
to
le?
operand
C
/=
A
is
equivalent
to
C
=
C
/
A
%=
Modulus
AND
assignment
operator,
It
takes
modulus
using
two
operands
and
assign
the
result
to
le?
operand
C
%=
A
is
equivalent
to
C
=
C
%
A
35. public static void main(String[] args) {
int a , b , c ;
a = 10 ;
b = 20 ;
c = 0 ;
c = a + b ;
System.out.println (c) ;
}
010101010101010101010101
30
Assignment Operators
a = 10 b = 20 c = 0
36. public static void main(String[] args) {
int a , b , c ;
a = 10 ;
b = 20 ;
c = 0 ;
c + = a ;
System.out.println (c) ;
}
010101010101010101010101
10
Assignment Operators
a = 10 b = 20 c = 0
37. public static void main(String[] args) {
int a , b , c ;
a = 10 ;
b = 20 ;
c = 0 ;
c - = a ;
System.out.println (c) ;
}
010101010101010101010101
-10
Assignment Operators
a = 10 b = 20 c = 0
38. public static void main(String[] args) {
int a , b , c ;
a = 10 ;
b = 20 ;
c = 0 ;
c*=a ;
System.out.println (c) ;
}
010101010101010101010101
0
Assignment Operators
a = 10 b = 20 c = 0
39. public static void main(String[] args) {
int a , b , c ;
a = 10 ;
b = 20 ;
c = 0 ;
c/=a ;
System.out.println (c) ;
}
010101010101010101010101
0
Assignment Operators
a = 10 b = 20 c = 0
40. public static void main(String[] args) {
int a , b , c ;
a = 10 ;
b = 20 ;
c = 0 ;
c%=a ;
System.out.println (c) ;
}
010101010101010101010101
0
Assignment Operators
a = 10 b = 20 c = 0