Operators in Java are symbols that perform specific operations on one, two, or three operands (variables, values, or expressions) and return a result. Java provides a rich set of operators that can be categorized into several types based on their functionality. Below is a detailed explanation of the different types of operators in Java.
Types of Operators in Java
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, and division.
Operator | Description | Example |
---|---|---|
+ | Addition | int sum = a + b; |
- | Subtraction | int diff = a - b; |
* | Multiplication | int prod = a * b; |
/ | Division | int quot = a / b; |
% | Modulus (Remainder) | int rem = a % b; |
2. Unary Operators
Unary operators operate on a single operand.
Operator | Description | Example |
---|---|---|
+ | Positive (unary plus) | int x = +5; |
- | Negative (unary minus) | int y = -5; |
++ | Increment (pre/post) | x++; or ++x; |
-- | Decrement (pre/post) | y--; or --y; |
! | Logical NOT (boolean negation) | boolean flag = !true; |
3. Relational Operators
Relational operators are used to compare two values and return a boolean result (true
or false
).
Operator | Description | Example |
---|---|---|
== | Equal to | if (a == b) |
!= | Not equal to | if (a != b) |
> | Greater than | if (a > b) |
< | Less than | if (a < b) |
>= | Greater than or equal to | if (a >= b) |
<= | Less than or equal to | if (a <= b) |
4. Logical Operators
Logical operators are used to combine multiple boolean expressions.
Operator | Description | Example |
---|---|---|
&& | Logical AND | if (a > 0 && b > 0) |
|| | Logical OR | if (a > 0 || b > 0) |
! | Logical NOT | if (!(a > 0)) |
5. Bitwise Operators
Bitwise operators perform operations on individual bits of integer types (byte
, short
, int
, long
).
Operator | Description | Example |
---|---|---|
& | Bitwise AND | int result = a & b; |
| | Bitwise OR | int result = a | b; |
^ | Bitwise XOR | int result = a ^ b; |
~ | Bitwise NOT (complement) | int result = ~a; |
<< | Left shift | int result = a << 1; |
>> | Right shift (signed) | int result = a >> 1; |
>>> | Right shift (unsigned) | int result = a >>> 1; |
6. Assignment Operators
Assignment operators are used to assign values to variables.
Operator | Description | Example |
---|---|---|
= | Simple assignment | int x = 5; |
+= | Add and assign | x += 5; (x = x + 5) |
-= | Subtract and assign | x -= 5; (x = x – 5) |
*= | Multiply and assign | x *= 5; (x = x * 5) |
/= | Divide and assign | x /= 5; (x = x / 5) |
%= | Modulus and assign | x %= 5; (x = x % 5) |
&= | Bitwise AND and assign | x &= 5; (x = x & 5) |
|= | Bitwise OR and assign | x |= 5; (x = x |
^= | Bitwise XOR and assign | x ^= 5; (x = x ^ 5) |
<<= | Left shift and assign | x <<= 1; (x = x << 1) |
>>= | Right shift (signed) and assign | x >>= 1; (x = x >> 1) |
>>>= | Right shift (unsigned) and assign | x >>>= 1; (x = x >>> 1) |
7. Ternary Operator (Conditional Operator)
The ternary operator is a shorthand for an if-else
statement.
Operator | Description | Example |
---|---|---|
? : | Ternary operator | int max = (a > b) ? a : b; |
8. instanceof Operator
The instanceof
operator is used to check whether an object is an instance of a specific class or interface.
Operator | Description | Example |
---|---|---|
instanceof | Checks object type | if (obj instanceof MyClass) |
Operator Precedence and Associativity
Operator precedence determines the order in which operators are evaluated in an expression. Associativity determines the order in which operators of the same precedence are evaluated.
Operator Precedence Table (Highest to Lowest)
Precedence | Operator Type | Operators |
---|---|---|
1 | Postfix | expr++ , expr-- |
2 | Unary | ++expr , --expr , +expr , -expr , ! , ~ |
3 | Multiplicative | * , / , % |
4 | Additive | + , - |
5 | Shift | << , >> , >>> |
6 | Relational | < , > , <= , >= , instanceof |
7 | Equality | == , != |
8 | Bitwise AND | & |
9 | Bitwise XOR | ^ |
10 | Bitwise OR | | |
11 | Logical AND | && |
12 | Logical OR | || |
13 | Ternary | ? : |
14 | Assignment | = , += , -= , *= , /= , etc. |
Example Program
public class Main {
public static void main(String[] args) {
int a = 10, b = 5;
// Arithmetic Operators
System.out.println("Sum: " + (a + b));
System.out.println("Difference: " + (a - b));
System.out.println("Product: " + (a * b));
System.out.println("Quotient: " + (a / b));
System.out.println("Remainder: " + (a % b));
// Unary Operators
System.out.println("Increment: " + (++a));
System.out.println("Decrement: " + (--b));
// Relational Operators
System.out.println("a > b: " + (a > b));
// Logical Operators
System.out.println("a > 0 && b > 0: " + (a > 0 && b > 0));
// Bitwise Operators
System.out.println("a & b: " + (a & b));
// Ternary Operator
int max = (a > b) ? a : b;
System.out.println("Max: " + max);
}
}
Summary
Operator Type | Key Operators | Use Case |
---|---|---|
Arithmetic | + , - , * , / , % | Basic mathematical operations |
Unary | + , - , ++ , -- , ! | Operations on a single operand |
Relational | == , != , > , < , >= , <= | Comparing values |
Logical | && , || , ! | Combining boolean expressions |
Bitwise | & , | , ^ , ~ , << , >> , >>> | Operations on bits |
Assignment | = , += , -= , *= , /= | Assigning values to variables |
Ternary | ? : | Shorthand for if-else |
instanceof | instanceof | Checking object type |
Understanding and using operators effectively is essential for writing efficient and concise Java programs.