Operators in Java
Operators are special symbols or keywords used to perform operations on variables and values in Java. They are categorized based on their functionality.
1. Arithmetic Operators
Used for mathematical operations.
| Operator | Description | Example |
|---|---|---|
| + | Addition | a + b |
| - | Subtraction | a - b |
| * | Multiplication | a * b |
| / | Division | a / b |
| % | Modulus | a % b |
public class ArithmeticOperators {
public static void main(String[] args) {
int a = 10, b = 5;
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. Relational Operators
Used to compare values.
| 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 |
public class RelationalOperators {
public static void main(String[] args) {
int a = 10, b = 5;
System.out.println("Equal to: " + (a == b));
System.out.println("Not equal to: " + (a != b));
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("Less than or equal to: " + (a <= b));
}
}
3. Logical Operators
Used to perform logical operations.
| Operator | Description | Example |
|---|---|---|
| && | Logical AND | a > b && a > 0 |
| || | Logical OR | a > b || b > 0 |
| ! | Logical NOT | !(a > b) |
public class LogicalOperators {
public static void main(String[] args) {
int a = 10, b = 5;
System.out.println("Logical AND: " + (a > b && a > 0));
System.out.println("Logical OR: " + (a > b || b > 0));
System.out.println("Logical NOT: " + !(a > b));
}
}
4. Assignment Operators
Used to assign values to variables.
| Operator | Description | Example |
|---|---|---|
| = | Simple assignment | int a = 10; |
| += | Add and assign | a += 5; // Equivalent to a = a + 5; |
| -= | Subtract and assign | a -= 3; // Equivalent to a = a - 3; |
| *= | Multiply and assign | a *= 2; // Equivalent to a = a * 2; |
| /= | Divide and assign | a /= 2; // Equivalent to a = a / 2; |
| %= | Modulus and assign | a %= 3; // Equivalent to a = a % 3; |
public class AssignmentOperators {
public static void main(String[] args) {
int a = 10, b = 5;
a += b; // a = a + b
System.out.println("Add and assign: " + a);
a -= b; // a = a - b
System.out.println("Subtract and assign: " + a);
a *= b; // a = a * b
System.out.println("Multiply and assign: " + a);
}
}
5. Increment and Decrement Operators
These operators are used to increase or decrease a variable by 1.
| Operator | Description | Example |
|---|---|---|
| ++ | Increment | a++ or ++a |
| -- | Decrement | a-- or --a |
public class IncrementDecrementExample {
public static void main(String[] args) {
int a = 10;
System.out.println("Post-Increment: " + (a++));
System.out.println("After Post-Increment: " + a);
System.out.println("Pre-Increment: " + (++a));
System.out.println("Post-Decrement: " + (a--));
System.out.println("After Post-Decrement: " + a);
System.out.println("Pre-Decrement: " + (--a));
}
}
6. Bitwise Operators
Bitwise operators are used to perform operations on individual bits.
| Operator | Description | Example |
|---|---|---|
| & | Bitwise AND | a & b |
| | | Bitwise OR | a | b |
| ^ | Bitwise XOR | a ^ b |
| ~ | Bitwise Complement | ~a |
| << | Left Shift | a << 2 |
| >> | Right Shift | a >> 2 |
public class BitwiseExample {
public static void main(String[] args) {
int a = 5, b = 3;
System.out.println("Bitwise AND: " + (a & b));
System.out.println("Bitwise OR: " + (a | b));
System.out.println("Bitwise XOR: " + (a ^ b));
System.out.println("Bitwise Complement: " + (~a));
System.out.println("Left Shift: " + (a << 1));
System.out.println("Right Shift: " + (a >> 1));
}
}
7. Ternary Operator
The ternary operator is a shorthand for the if-else statement.
| Operator | Description | Example |
|---|---|---|
| ? : | Shorthand for if-else | (condition) ? value_if_true : value_if_false |
public class TernaryExample {
public static void main(String[] args) {
int a = 10, b = 20;
String result = (a > b) ? "a is greater" : "b is greater";
System.out.println("Ternary Operator Result: " + result);
}
}