# Java Operators: Complete Guide
Java provides a rich set of operators that can be categorized into several groups:
## 1. Arithmetic Operators
```java
int a = 10, b = 3;
// Basic arithmetic
int sum = a + b; // 13 (Addition)
int difference = a - b; // 7 (Subtraction)
int product = a * b; // 30 (Multiplication)
int quotient = a / b; // 3 (Division - integer division)
int remainder = a % b; // 1 (Modulus - remainder after division)
// Unary operators
int negative = -a; // -10 (Unary minus)
int positive = +a; // 10 (Unary plus)
a++; // Post-increment (a becomes 11)
++a; // Pre-increment (a becomes 12)
b--; // Post-decrement (b becomes 2)
--b; // Pre-decrement (b becomes 1)
```
## 2. Assignment Operators
```java
int x = 5; // Simple assignment
// Compound assignments
x += 3; // Equivalent to x = x + 3 (now 8)
x -= 2; // Equivalent to x = x - 2 (now 6)
x *= 4; // Equivalent to x = x * 4 (now 24)
x /= 3; // Equivalent to x = x / 3 (now 8)
x %= 5; // Equivalent to x = x % 5 (now 3)
```
## 3. Relational Operators
```java
int p = 5, q = 7;
boolean isEqual = (p == q); // false
boolean notEqual = (p != q); // true
boolean lessThan = (p < q); // true
boolean greaterThan = (p > q); // false
boolean lessOrEqual = (p <= q); // true
boolean greaterOrEqual = (p >= q); // false
```
## 4. Logical Operators
```java
boolean t = true, f = false;
// Basic logical operations
boolean andResult = t && f; // false (Logical AND)
boolean orResult = t || f; // true (Logical OR)
boolean notResult = !t; // false (Logical NOT)
// Short-circuit evaluation
boolean result = (5 > 3) || (10/0 == 0); // No error due to short-circuit
```
## 5. Bitwise Operators
```java
int m = 5; // Binary: 0101
int n = 3; // Binary: 0011
int bitwiseAnd = m & n; // 0001 (1)
int bitwiseOr = m | n; // 0111 (7)
int bitwiseXor = m ^ n; // 0110 (6)
int bitwiseNot = ~m; // 11111111111111111111111111111010 (-6 in 32-bit)
int leftShift = m << 1; // 1010 (10)
int rightShift = m >> 1; // 0010 (2)
int unsignedRightShift = m >>> 1; // 0010 (2)
```
## 6. Ternary (Conditional) Operator
```java
int age = 20;
String status = (age >= 18) ? "Adult" : "Minor"; // "Adult"
```
## 7. Instanceof Operator
```java
Object obj = "Hello";
if (obj instanceof String) {
System.out.println("It's a String");
}
```
## 8. Operator Precedence Table
| Category | Operators | Associativity |
|----------------|------------------------------------|---------------|
| Postfix | expr++ expr-- | Left to right |
| Unary | ++expr --expr +expr -expr ~ ! | Right to left |
| Multiplicative | * / % | Left to right |
| Additive | + - | Left to right |
| Shift | << >> >>> | Left to right |
| Relational | < > <= >= instanceof | Left to right |
| Equality | == != | Left to right |
| Bitwise AND | & | Left to right |
| Bitwise XOR | ^ | Left to right |
| Bitwise OR | | | Left to right |
| Logical AND | && | Left to right |
| Logical OR | || | Left to right |
| Ternary | ? : | Right to left |
| Assignment | = += -= *= /= %= &= ^= |= <<= >>= >>>= | Right to left |
## Special Notes
1. **String Concatenation**: The `+` operator concatenates strings
```java
String name = "John";
String greeting = "Hello, " + name + "!"; // "Hello, John!"
```
2. **Type Promotion**: In arithmetic operations, Java automatically promotes smaller types to larger ones
```java
int i = 5;
double d = 6.2;
double result = i + d; // int promoted to double
```
3. **Compound Assignment Casting**: Compound assignments perform implicit casting
```java
byte b = 5;
b += 10; // Equivalent to b = (byte)(b + 10);
```
Would you like me to elaborate on any specific operator or provide more examples?
No comments:
Post a Comment