Introduction
This article explains the basic Java operators.
What is an Operator?
An operator is a method that manipulates values. Let's use the example of addition, in which we add two values (like 1+2) where the values are operands and the "+" sign is the operator, in other words, the operator is a method used to operate on operands.
Operators in Java
Java provides various types of operators to manipulate various values.
- Arithmetic Operators
- Relational Operators
- Bitwise Operators.
- Logical Operators.
- Assignment Operators.
- Miscellaneous Operators.
1. Arithmetic Operators
It is used in mathematical expressions in the same way it is used in algebra. It includes the following:
- Addition (+)
- Subtraction (-)
- Manipulation (*)
- Division (/)
- Modulus (%)
- Increment (++)
- Decrement (--)
Example
ArithmeticEx.java
- public class ArithmeticEx
- {
- public static void main(String args[])
- {
- int i = 15;
- int j = 25;
- int k = 35;
- int l = 10;
- System.out.println("i + j = " + (i + j));
- System.out.println("i - j = " + (i - j));
- System.out.println("i * j = " + (i * j));
- System.out.println("j / i = " + (j / i));
- System.out.println("j % i = " + (j % i));
- System.out.println("k % i = " + (k % i));
- System.out.println("i++ = " + (i++));
- System.out.println("j-- = " + (i--));
- }
- }
Output

2. Relational Operators
This operator is used to compare two values.
- > (Greater Than operator).
- < (Less Than operator).
- == (EqualTo operator)
- != (Not EqualsTo operator; Work same as EqualTo operator).
- >= (Greater than EuqalTo).
- <= (Less Than EqualTo).
Example
RelationalOperatorEx.java
- public class RelationalOperatorEx
- {
- public static void main(String args[])
- {
- int i = 10;
- int j = 20;
- System.out.println("i == j = " + (i == j));
- System.out.println("i != j = " + (i != j));
- System.out.println("i > j = " + (i > j));
- System.out.println("i < j = " + (i < j));
- System.out.println("j >= i = " + (j >= i));
- System.out.println("j <= i = " + (j <= i));
- }
- }
Output

3. Bitwise Operator
This operator works on bits to perform bit by bit operations.
- >>> (Shift right zero fill operaotr).
- << (Binary Left Shift Operator).
- >> (binary right shift operator)
- && (Binary and operator)
- || (Binary Or).
- ^ (Binary XOR).
- ~ (Binary Ones Complement; used to flipping bits).
Example
- BitwiseOperatorEx.java
- public class BitwiseOperatorEx
- {
- public static void main(String args[])
- {
- int i = 45;
- int l = 15;
- int k = 0;
- k = i & l;
- System.out.println("i & l = " + k);
- k = i | l;
- System.out.println("i | l = " + k);
- k = i ^ l;
- System.out.println("i ^ l = " + k);
- k = ~i;
- System.out.println("~i = " + k);
- k = i << 2;
- System.out.println("i << 2 = " + k);
- k = i >> 2;
- System.out.println("i >> 2 = " + k);
- k = i >>> 2;
- System.out.println("i >>> 2 = " + k);
- }
- }




Join the conversation! Your thoughts help the community grow.