Operators in Java
Introduction
Java is one of the most popular programming languages used for developing web applications, enterprise applications, mobile applications, and backend systems.
In Java programming, operators are special symbols that are used to perform operations on variables and values. Operators help us perform calculations, compare values, assign values, and make logical decisions.
For example:
+operator is used for addition==operator is used for comparison&&operator is used for logical operations
Understanding operators is one of the fundamental concepts in Java because they are used in almost every Java program.
Types of Operators in Java
Java operators are classified into the following categories:
Arithmetic Operators
Assignment Operators
Relational Operators
Logical Operators
Unary Operators
Bitwise Operators
Ternary Operator
Shift Operators
1. Arithmetic Operators in Java
Arithmetic operators are used to perform mathematical operations.
Arithmetic Operators
| Operator | Description |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulus |
Example
public class ArithmeticExample {
public static void main(String[] args) {
int a = 20;
int b = 10;
System.out.println("Addition: " + (a + b));
System.out.println("Subtraction: " + (a - b));
System.out.println("Multiplication: " + (a * b));
System.out.println("Division: " + (a / b));
System.out.println("Modulus: " + (a % b));
}
}
Output
Addition: 30
Subtraction: 10
Multiplication: 200
Division: 2
Modulus: 0
2. Assignment Operators in Java
Assignment operators are used to assign values to variables.
Assignment Operators
| Operator | Example |
|---|---|
| = | a = 10 |
| += | a += 5 |
| -= | a -= 5 |
| *= | a *= 5 |
| /= | a /= 5 |
Example
public class AssignmentExample {
public static void main(String[] args) {
int number = 10;
number += 5;
System.out.println(number);
}
}
Output
15
Explanation:
number += 5;
is equal to:
number = number + 5;
3. Relational Operators in Java
Relational operators are used to compare two values.
The result will always be either true or false.
Relational Operators
| Operator | Description |
|---|---|
| == | Equal to |
| != | Not equal |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal |
| <= | Less than or equal |
Example
public class RelationalExample {
public static void main(String[] args) {
int age = 20;
System.out.println(age >= 18);
System.out.println(age == 25);
}
}
Output
true
false
4. Logical Operators in Java
Logical operators are used to combine multiple conditions.
Logical Operators
| Operator | Description |
|---|---|
| && | Logical AND |
| ! | Logical NOT |
Example
public class LogicalExample {
public static void main(String[] args) {
int age = 25;
boolean hasID = true;
System.out.println(age >= 18 && hasID);
}
}
Output
true
Explanation:
The && operator returns true only when both conditions are true.
5. Unary Operators in Java
Unary operators work with only one operand.
Unary Operators
| Operator | Description |
|---|---|
| ++ | Increment |
| -- | Decrement |
| + | Positive |
| - | Negative |
| ! | Reverse condition |
Example
public class UnaryExample {
public static void main(String[] args) {
int count = 5;
count++;
System.out.println(count);
}
}
Output
6
6. Bitwise Operators in Java
Bitwise operators perform operations on binary values.
Bitwise Operators
| Operator | Description |
|---|---|
| & | Bitwise AND |
| ^ | Bitwise XOR |
| ~ | Bitwise Complement |
Example
public class BitwiseExample {
public static void main(String[] args) {
int a = 5;
int b = 3;
System.out.println(a & b);
}
}
Output
1
Explanation:
Binary representation:
5 = 101
3 = 011
AND
101
011
---
001
Result:
1
7. Ternary Operator in Java
The ternary operator is a shortcut for the if-else statement.
Syntax
condition ? value1 : value2;
Example
public class TernaryExample {
public static void main(String[] args) {
int age = 20;
String result = (age >= 18) ? "Adult" : "Minor";
System.out.println(result);
}
}
Output
Adult
8. Shift Operators in Java
Shift operators are used to shift bits left or right.
Shift Operators
| Operator | Description |
|---|---|
| << | Left shift |
| >> | Right shift |
| >>> | Unsigned right shift |
Example
public class ShiftExample {
public static void main(String[] args) {
int number = 8;
System.out.println(number << 2);
}
}
Output
32
Explanation:
8 = 1000
After left shift by 2:
100000 = 32
Operator Precedence in Java
When multiple operators are used in a single expression, Java follows operator precedence rules.
Example:
public class PrecedenceExample {
public static void main(String[] args) {
int result = 10 + 5 * 2;
System.out.println(result);
}
}
Output
20
Explanation:
Multiplication has higher priority than addition.
Execution:
5 * 2 = 10
10 + 10 = 20
Real-Time Example Using Operators
Example: Employee Eligibility Check
public class EligibilityCheck {
public static void main(String[] args) {
int age = 22;
boolean graduation = true;
if(age >= 18 && graduation) {
System.out.println("Eligible");
} else {
System.out.println("Not Eligible");
}
}
}
Output
Eligible
Used operators:
Relational operator →
>=Logical operator →
&&
Conclusion
Operators are one of the important building blocks of Java programming.
They are used for:
Performing calculations
Comparing values
Making logical decisions
Controlling program execution
Working with binary data
A strong understanding of operators helps developers write efficient and logical Java programs.