What are Operators in Java?
In Java, operators are special symbols or keywords that perform specific operations on one or more operands (values or variables) to produce a result. Operators are the building blocks of Java expressions and play a crucial role in manipulating data and controlling program flow.
Types of Java Operators
Java provides a wide variety of operators categorized into the following groups:
- Arithmetic Operators
- Assignment Operators
- Relational (Comparison) Operators
- Logical Operators
- Bitwise Operators
- Unary Operators
- Ternary Operator
- Shift Operators
Let’s explore each category in detail with examples.
1. Arithmetic Operators
Arithmetic operators perform basic mathematical operations such as addition, subtraction, multiplication, and division.
Operator | Description | Example |
---|---|---|
+ | Addition | a + b |
– | Subtraction | a – b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus (Remainder) | a % b |
Example Code:
public class ArithmeticExample {
public static void main(String[] args) {
int a = 10, b = 3;
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));
}
}
2. Assignment Operators
Assignment operators assign values to variables.
Operator | Description | Example |
---|---|---|
= | Assigns a value | a = b |
+= | Adds and assigns | a += b |
-= | Subtracts and assigns | a -= b |
*= | Multiplies and assigns | a *= b |
/= | Divides and assigns | a /= b |
%= | Calculates remainder and assigns | a %= b |
Example Code:
public class AssignmentExample {
public static void main(String[] args) {
int a = 5;
a += 2; // a = a + 2
System.out.println("New Value of a: " + a);
}
}
3. Relational (Comparison) Operators
Relational operators compare two values and return a boolean result (true or false).
Operator | Description | Example |
---|---|---|
== | Equal to | a == b |
!= | Not equal to | a != b |
> | Greater than | a > b |
< | Less than | a < b |
>= | Greater than or equal to | a >= b |
<= | Less than or equal to | a <= b |
Example Code:
public class RelationalExample {
public static void main(String[] args) {
int x = 10, y = 20;
System.out.println("x == y: " + (x == y));
System.out.println("x > y: " + (x > y));
}
}
4. Logical Operators
Logical operators are used to perform logical operations, usually with boolean values.
Operator | Description | Example |
---|---|---|
&& | Logical AND | (a > b) && (b < c) |
` | ` | |
! | Logical NOT | !(a > b) |
Example Code:
public class LogicalExample {
public static void main(String[] args) {
boolean isTrue = (5 > 2) && (3 < 5);
System.out.println("Result: " + isTrue);
}
}
5. Bitwise Operators
Bitwise operators perform operations at the binary level.
Operator | Description | Example |
---|---|---|
& | Bitwise AND | a & b |
` | ` | Bitwise OR |
^ | Bitwise XOR | a ^ b |
~ | Bitwise Complement | ~a |
Example Code:
public class BitwiseExample {
public static void main(String[] args) {
int a = 5, b = 3; // Binary: a=101, b=011
System.out.println("Bitwise AND: " + (a & b));
}
}
6. Unary Operators
Unary operators operate on a single operand.
Operator | Description | Example |
---|---|---|
+ | Positive | +a |
– | Negative | -a |
++ | Increment | ++a |
— | Decrement | –a |
! | Logical Complement | !a |
Example Code:
public class UnaryExample {
public static void main(String[] args) {
int x = 5;
System.out.println("Increment: " + (++x));
System.out.println("Decrement: " + (--x));
}
}
7. Ternary Operator
The ternary operator is a shorthand for if-else statements.
Syntax:
condition ? expression1 : expression2
Example Code:
public class TernaryExample {
public static void main(String[] args) {
int a = 10, b = 20;
int max = (a > b) ? a : b;
System.out.println("Maximum: " + max);
}
}
8. Shift Operators
Shift operators shift the bits of a number.
Operator | Description | Example |
---|---|---|
<< | Left Shift | a << 2 |
>> | Right Shift | a >> 2 |
>>> | Unsigned Right Shift | a >>> 2 |
Example Code:
public class ShiftExample {
public static void main(String[] args) {
int num = 8; // Binary: 1000
System.out.println("Left Shift: " + (num << 1)); // Binary: 10000
}
}