What are Operators in Java?
Operators are special symbols used to perform operations on variables and values.
Java operators are used for mathematical calculations, comparisons, conditions, assignments, and logical operations.
Types of Java Operators
- Arithmetic Operators
- Assignment Operators
- Relational Operators
- Logical Operators
- Unary Operators
- Bitwise Operators
- Ternary Operator
Arithmetic Operators
Arithmetic operators are used for mathematical calculations.
| Operator | Description | Example |
|---|---|---|
| + | Addition | a + b |
| - | Subtraction | a - b |
| * | Multiplication | a * b |
| / | Division | a / b |
| % | Modulus | a % b |
Example
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 5;
System.out.println(a + b);
System.out.println(a - b);
System.out.println(a * b);
System.out.println(a / b);
System.out.println(a % b);
}
}
Output
15 5 50 2 0
Assignment Operators
Assignment operators are used to assign values to variables.
| Operator | Description | Example |
|---|---|---|
| = | Assign value | x = 10 |
| += | Add and assign | x += 5 |
| -= | Subtract and assign | x -= 2 |
| *= | Multiply and assign | x *= 3 |
| /= | Divide and assign | x /= 2 |
Example
int x = 10; x += 5; System.out.println(x); x -= 2; System.out.println(x); x *= 3; System.out.println(x);
Output
15 13 39
Relational Operators
Relational operators compare two values and return 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
int a = 10; int b = 20; System.out.println(a == b); System.out.println(a != b); System.out.println(a < b); System.out.println(a > b);
Output
false true true false
Logical Operators
Logical operators are used with boolean values.
| Operator | Description | Example |
|---|---|---|
| && | Logical AND | a > 5 && b > 5 |
| || | Logical OR | a > 5 || b > 5 |
| ! | Logical NOT | !(a > b) |
Example
int age = 20; System.out.println(age > 18 && age < 30); System.out.println(age > 50 || age < 25); System.out.println(!(age > 18));
Unary Operators
Unary operators work on a single operand.
| Operator | Description |
|---|---|
| ++ | Increment |
| -- | Decrement |
Example
int x = 5; x++; System.out.println(x); x--; System.out.println(x);
Output
6 5
Bitwise Operators
Bitwise operators perform operations at bit level.
| Operator | Description |
|---|---|
| & | Bitwise AND |
| | | Bitwise OR |
| ^ | Bitwise XOR |
Ternary Operator
Ternary operator is a shorthand form of if-else statement.
int age = 18; String result = (age >= 18) ? "Adult" : "Minor"; System.out.println(result);
Output
Adult
Operator Precedence
Operator precedence determines which operation is executed first.
int result = 10 + 5 * 2; System.out.println(result);
Output
20
Multiplication is executed before addition.
Complete Java Operators Program
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 5;
System.out.println(a + b);
System.out.println(a > b);
System.out.println(a == b);
System.out.println(a != b);
a++;
System.out.println(a);
}
}
Best Practices
- Use meaningful variable names
- Use parentheses for better readability
- Avoid complex expressions
- Understand operator precedence properly
- Use logical operators carefully
Common Errors
- Using = instead of ==
- Division by zero
- Incorrect operator precedence
- Using wrong logical operators
Conclusion
Java operators are essential for performing calculations, comparisons, logical operations, and assignments. Understanding operators is very important for writing efficient Java programs.