Operators in Java are symbols used to perform operations on variables and values. They are essential building blocks in programming, enabling computations, comparisons, and decision-making.
Types of Operators in Java
Java provides the following categories of operators:
- Arithmetic Operators
- Relational (Comparison) Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Unary Operators
- Ternary Operator
- Instanceof Operator
1. Arithmetic Operators
Arithmetic operators perform basic mathematical operations.
Operator | Operation | Example | Result |
---|---|---|---|
+ |
Addition | a + b |
Sum of a and b . |
- |
Subtraction | a - b |
Difference of a and b . |
* |
Multiplication | a * b |
Product of a and b . |
/ |
Division | a / b |
Quotient of a and b . |
% |
Modulus | a % b |
Remainder of a/b . |
Example:
public class ArithmeticExample { public static void main(String[] args) { int a = 10, b = 3; System.out.println("Addition: " + (a + b)); // 13 System.out.println("Subtraction: " + (a - b)); // 7 System.out.println("Multiplication: " + (a * b)); // 30 System.out.println("Division: " + (a / b)); // 3 System.out.println("Modulus: " + (a % b)); // 1 } }
2. Relational (Comparison) Operators
Relational operators compare two values and return a boolean result (true
or false
).
Operator | Meaning | Example |
---|---|---|
== |
Equal to | a == b |
!= |
Not equal to | a != b |
> |
Greater than | a > b |
< |
Less than | a < b |
>= |
Greater than or equal | a >= b |
<= |
Less than or equal | a <= b |
Example:
public class RelationalExample { public static void main(String[] args) { int a = 10, b = 5; System.out.println("a == b: " + (a == b)); // false System.out.println("a > b: " + (a > b)); // true } }
3. Logical Operators
Logical operators are used to combine multiple boolean expressions.
Operator | Meaning | Example |
---|---|---|
&& |
Logical AND | (a > b) && (a > c) |
` | ` | |
! |
Logical NOT | !(a > b) |
Example:
public class LogicalExample { public static void main(String[] args) { boolean x = true, y = false; System.out.println("x && y: " + (x && y)); // false System.out.println("x || y: " + (x || y)); // true System.out.println("!x: " + (!x)); // false } }
4. Bitwise Operators
Bitwise operators work on bits and perform operations bit by bit.
Operator | Meaning | Example |
---|---|---|
& |
Bitwise AND | a & b |
` | ` | Bitwise OR |
^ |
Bitwise XOR | a ^ b |
~ |
Bitwise Complement | ~a |
<< |
Left shift | a << 2 |
>> |
Right shift | a >> 2 |
Example:
public class BitwiseExample { public static void main(String[] args) { int a = 5, b = 3; // Binary: 5=0101, 3=0011 System.out.println("a & b: " + (a & b)); // 1 System.out.println("a | b: " + (a | b)); // 7 } }
5. Assignment Operators
Assignment operators are used to assign values to variables.
Operator | Meaning | Example |
---|---|---|
= |
Assign | a = 10 |
+= |
Add and assign | a += 5 (a = 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
Unary operators operate on a single operand.
Operator | Meaning | Example |
---|---|---|
+ |
Positive number | +a |
- |
Negative number | -a |
++ |
Increment (pre/post) | ++a / a++ |
-- |
Decrement (pre/post) | --a / a-- |
! |
Logical NOT | !a |
Example:
public class UnaryExample { public static void main(String[] args) { int a = 10; System.out.println("++a: " + (++a)); // 11 System.out.println("a--: " + (a--)); // 11 } }
7. Ternary Operator
The ternary operator is a shorthand for if-else
conditions.
Syntax:
variable = (condition) ? expression1 : expression2;
Example:
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. Instanceof Operator
The instanceof
operator checks if an object belongs to a specific class or subclass.
Example:
public class InstanceofExample { public static void main(String[] args) { String s = "Java"; System.out.println(s instanceof String); // true } }
Key Points
- Operator Precedence: Operators follow a specific order of evaluation. For example, multiplication (
*
) takes precedence over addition (+
). - Associativity: Operators with the same precedence level are evaluated based on associativity (left-to-right or right-to-left).
Example of Operator Precedence:
public class PrecedenceExample { public static void main(String[] args) { int result = 10 + 5 * 2; // Multiplication (*) is evaluated first System.out.println("Result: " + result); // Output: 20 } }