Operators
## Learning Objectives
- Master arithmetic operators
- Understand comparison operators
- Learn logical operators
- Work with bitwise operators
## Arithmetic Operators
### Basic Operations
```java
int a = 10, b = 3;
System.out.println(a + b); // 13 (addition)
System.out.println(a - b); // 7 (subtraction)
System.out.println(a * b); // 30 (multiplication)
System.out.println(a / b); // 3 (integer division)
System.out.println(a % b); // 1 (modulus/remainder)
```
### Division Behavior
```java
// Integer division truncates
System.out.println(10 / 3); // 3, not 3.333
// For floating-point
System.out.println(10.0 / 3.0); // 3.333...
```
### Increment/Decrement
```java
int x = 5;
// Pre-increment/decrement
System.out.println(++x); // 6 (increments first)
System.out.println(--x); // 5 (decrements first)
// Post-increment/decrement
System.out.println(x++); // 5 (uses value, then increments)
System.out.println(x); // 6 (now incremented)
int y = 5;
System.out.println(y--); // 5 (uses value, then decrements)
System.out.println(y); // 4
```
## Comparison Operators
### Relational Operators
```java
int a = 5, b = 10;
System.out.println(a == b); // false (equal)
System.out.println(a != b); // true (not equal)
System.out.println(a < b); // true (less than)
System.out.println(a > b); // false (greater than)
System.out.println(a <= b); // true (less or equal)
System.out.println(a >= b); // false (greater or equal)
```
### Don't Confuse
```java
// = is assignment
// == is comparison
int x = 5;
// if (x = 10) // WRONG! This assigns 10 to x
if (x == 5) // CORRECT! This compares
```
## Logical Operators
### AND, OR, NOT
```java
boolean a = true, b = false;
System.out.println(a && b); // false (AND - both must be true)
System.out.println(a || b); // true (OR - one must be true)
System.out.println(!a); // false (NOT - inverts)
```
### Short-Circuit Evaluation
```java
// && stops if first is false
if (x != 0 && y / x > 2) { // Safe - won't divide by zero
}
// || stops if first is true
if (obj == null || obj.isValid()) { // Safe - won't call on null
}
```
### Truth Table
| A | B | A && B | A \|\| B | !A |
|---|---|--------|----------|-----|
| T | T | T | T | F |
| T | F | F | T | F |
| F | T | F | T | T |
| F | F | F | F | T |
## Assignment Operators
### Simple Assignment
```java
int x = 10;
```
### Compound Assignment
```java
int x = 10;
x += 5; // x = 15 (x = x + 5)
x -= 3; // x = 12 (x = x - 3)
x *= 2; // x = 24 (x = x * 2)
x /= 4; // x = 6 (x = x / 4)
x %= 5; // x = 1 (x = x % 5)
```
## Bitwise Operators
### Binary Operations
```java
int a = 5, b = 3; // 101 and 011 in binary
System.out.println(a & b); // 1 (AND: 101 & 011 = 001)
System.out.println(a | b); // 7 (OR: 101 | 011 = 111)
System.out.println(a ^ b); // 6 (XOR: 101 ^ 011 = 110)
System.out.println(~a); // -6 (NOT: ~101 = ...010 = -6)
System.out.println(a << 1); // 10 (left shift: 101 << = 1010)
System.out.println(a >> 1); // 2 (right shift: 101 >> = 10)
```
### Common Uses
```java
// Check if bit is set
int flags = 0b1010; // 10 in decimal
if ((flags & 0b1000) != 0) {
System.out.println("Bit 3 is set");
}
// Set a bit
flags = flags | 0b0001;
// Toggle a bit
flags = flags ^ 0b0100;
```
## Ternary Operator
### Syntax
```java
// condition ? valueIfTrue : valueIfFalse
int max = (a > b) ? a : b;
String status = (age >= 18) ? "adult" : "minor";
```
## Operator Precedence
### Highest to Lowest
| Priority | Operators |
|----------|-------------------|
| 1 | `()` |
| 2 | `++`, `--`, `!` |
| 3 | `*`, `/`, `%` |
| 4 | `+`, `-` |
| 5 | `<`, `>`, `<=`, `>=` |
| 6 | `==`, `!=` |
| 7 | &, ^, | (bitwise) |
| 8 | `&&` |
| 9 | `\|\|` |
| 10 | `?:` |
| 11 | `=`, `+=`, `-=`, etc. |
### Use Parentheses
```java
// Clear precedence
if ((a > b) && (c < d)) {
}
// Instead of relying on memory
if (a > b && c < d) { // Same, but less clear
}
```
## Summary
- Arithmetic: `+`, `-`, `*`, `/`, `%`
- Increment/Decrement: `++x`, `x++`, `--x`, `x--`
- Comparison: `==`, `!=`, `<`, `>`, `<=`, `>=`
- Logical: `&&`, `||`, `!`
- Assignment: `=`, `+=`, `-=`, etc.
- Bitwise: `&`, `|`, `^`, `~`, `<<`, `>>`
- Ternary: `condition ? true : false`
- Use parentheses to clarify precedence
Comments
Comments powered by Giscus
To enable comments, add your Giscus embed code here.
Learn more about Giscus →