What is Operator?
- Operators are used to perform a operation on operands.
Types of Operators:
- Arithmetic Operator
- Assignment Operator
- Relational Operator
- Logical Operator
- Unary Operator
- Ternary Operator
- Shift Operator
- Bitwise Operator
Arithmetic Operator:
| Operator | Description |
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus (Remainder) |
class Arithmetic { public static void main (String[] args) { int a=100; int b=20; System.out.println("Addition:"+(a+b)); System.out.println("Sub:"+(a-b)); System.out.println("Multiply:"+(a*b)); System.out.println("Divide:"+(a/b)); System.out.println("Modulo:"+(a%b)); } }
Addition:120 Sub:80 Multiply:2000 Divide:5 Modulo:0
Assignment Operator:
- Used to assign values to a variable.
| Operator | Example |
= | a = 10 |
+= | a += 5 |
-= | a -= 5 |
*= | a *= 5 |
/= | a /= 5 |
%= | a %= 5 |
class Assignment { public static void main (String[] args) { int a; System.out.println("Assigning:"+(a = 10)); System.out.println("Add:"+(a += 20)); System.out.println("Sub:"+(a -= 10)); System.out.println("Multiply:"+(a *= 10)); System.out.println("Divide:"+(a /= 10)); System.out.println("Modulo:"+(a %= 3)); } }
Assigning:10 Add:30 Sub:20 Multiply:200 Divide:20 Modulo:2
Relational Operator:
- Used to compare two values.
| Operator | Meaning |
== | Equal to |
!= | Not Equal to |
> | Greater Than |
< | Less Than |
>= | Greater Than or Equal |
<= | Less Than or Equal |
class Relational { public static void main (String[] args) { int a=10; int b=30; System.out.println("Equal to:"+(a == b)); System.out.println("Not Equal to:"+(a != 20)); System.out.println("Greater than:"+(a > b)); System.out.println("Less than:"+(a<b)); System.out.println("Greater than or Equal to:"+(a >= b)); System.out.println("Greater than or Equal to:"+(a <= b)); } }
Equal to:false Not Equal to:true Greater than:false Less than:true Greater than or Equal to:false Greater than or Equal to:true
Logical Operator:

class Logical { public static void main (String[] args) { int a=10; int b=30; System.out.println("AND:"+((a > b) && (a != 20))); System.out.println("OR:"+((a == 20) || (b>a))); System.out.println("NOT:"+(a != b)); } }
AND:false OR:true NOT:true
Unary Operator:
| Operator | Meaning |
+ | Unary Plus |
- | Unary Minus |
++ | Increment |
-- | Decrement |
! | Logical NOT |
class Unary { public static void main (String[] args) { int a=10; int b=30; System.out.println("Unary Plus:"+(a+b)); System.out.println("Unary Minus:"+(a-b)); System.out.println("Increment:"+(++a)); System.out.println("Decrement:"+(a--)); System.out.println("Logical Not:"+(!true)); } }
Unary Plus:40 Unary Minus:-20 Increment:11 Decrement:11 Logical Not:false
Ternary Operator:
A shortcut for if-else.
Syntax
condition ? value1 : value2;
class Ternary { public static void main (String[] args) { int a=10; int b=30; String result = (a>b ? "A is Larger" : "B is Larger"); System.out.println("Which number is Larger?"+result); } }
Which number is Larger? B is Larger