In Java, operators are used to perform different kinds of operations on variables and values. Instead of writing long logic, operators help us do things quickly like calculations, comparisons, and decision-making.
For example:
int a = 10;
int b = 5;
int result = a + b;
Here, + is an operator that adds two values.
Types of Operators in Java
Java has several types of operators, each used for a specific purpose.
1. Arithmetic Operators
These are used for basic mathematical operations.
-
+→ Addition -
-→ Subtraction -
*→ Multiplication -
/→ Division -
%→ Remainder
Example:
int a = 10, b = 3;
System.out.println(a + b); // 13
System.out.println(a % b); // 1
2. Relational Operators
Used to compare two values. The result will always be true or false.
-
==,!=,>,<,>=,<=
Example:
int a = 10, b = 5;
System.out.println(a > b); // true
3. Logical Operators
Used when working with multiple conditions.
-
&&→ AND -
||→ OR -
!→ NOT
Example:
int age = 20;
System.out.println(age > 18 && age < 30);
4. Assignment Operators
Used to assign and update values.
-
=,+=,-=,*=,/=
Example:
int x = 5;
x += 3; // x = x + 3
5. Increment and Decrement
Used to increase or decrease value by 1.
-
++,--
Example:
int x = 5;
x++;
x--;
6. Ternary Operator
Shortcut for if-else.
int a = 10, b = 20;
int max = (a > b) ? a : b;
Bitwise Operators
Bitwise operators work on binary values (0s and 1s). They are mostly used in advanced or performance-based programs.
-
&→ AND -
|→ OR -
^→ XOR -
~→ NOT -
<<→ Left Shift -
>>→ Right Shift
Example:
int a = 5; // 0101
int b = 3; // 0011
System.out.println(a & b); // 1
System.out.println(a | b); // 7
Shift Example:
int x = 4;
System.out.println(x << 1); // 8
System.out.println(x >> 1); // 2
Special Operators
These are slightly different but important in Java.
1. instanceof
Checks object type.
String name = "Java";
System.out.println(name instanceof String);
2. Dot Operator (.)
Used to access methods or properties.
System.out.println("Hello".length());
3. Type Casting
Used to convert one datatype to another.
double d = 10.5;
int i = (int) d;
Final Thoughts
Operators are the core part of Java programming. Whether it is calculation, comparison, or decision-making, operators make coding easier and faster.
Once you practice using them in small programs, you will naturally understand where to use each type.