Operators in Java are special symbols or keywords used to perform operations on variables and values. They form the backbone of most logical and mathematical operations in Java. Below is a detailed discussion on Java operators with examples.
Types of Operators in Java
- Arithmetic Operators
- Relational (Comparison) Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Unary Operators
- Ternary Operator
- Special Operators
1. Arithmetic Operators
These operators are used to perform basic mathematical operations.
Operator | Description | Example |
+ | Addition | a + b |
– | Subtraction | a – b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus (remainder) | a % b |
Example:
public class ArithmeticOperators {
public static void main(String[] args) {
int a = 10, b = 3;
System.out.println(“Addition: ” + (a + b)); // Output: 13
System.out.println(“Subtraction: ” + (a – b)); // Output: 7
System.out.println(“Multiplication: ” + (a * b)); // Output: 30
System.out.println(“Division: ” + (a / b)); // Output: 3
System.out.println(“Modulus: ” + (a % b)); // Output: 1
}
}
2. Relational (Comparison) Operators
These operators are used to compare two values and return a boolean result.
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:
public class RelationalOperators {
public static void main(String[] args) {
int a = 10, b = 20;
System.out.println(a == b); // false
System.out.println(a != b); // true
System.out.println(a > b); // false
System.out.println(a < b); // true
System.out.println(a >= b); // false
System.out.println(a <= b); // true
}
}
3. Logical Operators
These are used to combine multiple boolean expressions.
Operator | Description | Example |
&& | Logical AND | a > 5 && b < 10 |
` | ` | |
! | Logical NOT | !(a > 5) |
Example:
public class LogicalOperators {
public static void main(String[] args) {
int a = 10, b = 5;
System.out.println((a > 5) && (b < 10)); // true
System.out.println((a > 15) || (b < 10)); // true
System.out.println(!(a > 15)); // true
}
}
4. Bitwise Operators
Operate at the bit level.
Operator | Description | Example |
& | Bitwise AND | a & b |
` | ` | Bitwise OR |
^ | Bitwise XOR | a ^ b |
~ | Bitwise Complement | ~a |
<< | Left shift | a << 2 |
>> | Right shift | a >> 2 |
>>> | Unsigned right shift | a >>> 2 |
Example:
public class BitwiseOperators {
public static void main(String[] args) {
int a = 5; // 0101 in binary
int b = 3; // 0011 in binary
System.out.println(“a & b: ” + (a & b)); // 0001 => 1
System.out.println(“a | b: ” + (a | b)); // 0111 => 7
System.out.println(“a ^ b: ” + (a ^ b)); // 0110 => 6
System.out.println(“~a: ” + (~a)); // -6
System.out.println(“a << 1: ” + (a << 1)); // 1010 => 10
System.out.println(“a >> 1: ” + (a >> 1)); // 0010 => 2
}
}
5. Assignment Operators
Used to assign values to variables.
Operator | Description | Example |
= | Simple assignment | a = 10 |
+= | Add and assign | a += 5 |
-= | Subtract and assign | a -= 5 |
*= | Multiply and assign | a *= 5 |
/= | Divide and assign | a /= 5 |
%= | Modulus and assign | a %= 5 |
6. Unary Operators
These operators require only one operand.
Operator | Description | Example |
+ | Positive | +a |
– | Negative | -a |
++ | Increment | ++a |
— | Decrement | –a |
! | Logical complement (NOT) | !a |
Example:
public class UnaryOperators {
public static void main(String[] args) {
int a = 10;
System.out.println(“Initial a: ” + a); // 10
System.out.println(“Post-increment: ” + a++); // 10
System.out.println(“After post-increment: ” + a); // 11
System.out.println(“Pre-increment: ” + ++a); // 12
}
}
7. Ternary Operator
This is a shorthand for if-else.
Syntax | Description |
condition ? value1 : value2 | If condition is true, returns value1; otherwise value2. |
Example:
public class TernaryOperator {
public static void main(String[] args) {
int a = 10, b = 20;
int max = (a > b) ? a : b;
System.out.println(“Maximum: ” + max); // Output: 20
}
}
8. Special Operators
- Instanceof Operator: Used to test whether an object is an instance of a specific class or subclass.
String s = “Hello”;
System.out.println(s instanceof String); // true
- Type Cast Operator: Converts data types explicitly.
int x = (int) 12.34; // Cast double to int
Summary
Java operators provide a powerful way to perform calculations, logic, and bit-level manipulations. Understanding their usage is crucial for writing efficient and readable code