1. Operators áááŻáᏠááŹáá˛?
An operator is a symbol that performs operations on variables and values.
int a = 10;
int b = 5;
int sum = a + b; // + is an operator
int product = a * b; // * is an operator
boolean result = a > b; // > is an operator
System.out.println("Sum: " + sum); // 15
System.out.println("Product: " + product); // 50
System.out.println("a > b: " + result); // true
Operator áááŻáᏠvariables áá˛áˇ values áá˝áą áĄááąáŤáşáážáŹ operations ááŻááşáá˛áˇ special symbols áá˝áą ááźá áşááŤáááşá
- Operands: Operator á operate ááŻááşáá˛áˇ values (a, b)
- Expression: Operators áá˛áˇ operands ááąáŤááşá¸ááŹá¸áᏠ(a + b)
- Result: Expression áá˛áˇ final value
Example: 10 + 5 - 10 áá˛áˇ 5 á operands, + á operator, 15 á
result
Arithmetic
Math operations: +, -, *, /, %
Relational
Comparisons: ==, !=, >, <,>=, <=< /p>
Logical
Logic: &&, ||, !
2. Arithmetic Operators (ááááşá¸ áá˝ááşááťááşáážáŻ)
Used for mathematical calculations.
| Operator | Name | Example | Result |
|---|---|---|---|
+ |
Addition (ááąáŤááşá¸ááźááşá¸) | 10 + 5
|
15 |
- |
Subtraction (ááŻááşááźááşá¸) | 10 - 5
|
5 |
* |
Multiplication (ááźážáąáŹááşááźááşá¸) | 10 * 5
|
50 |
/ |
Division (á áŹá¸ááźááşá¸) | 10 / 5
|
2 |
% |
Modulus (á áŹá¸á ááťááş) | 10 % 3
|
1 |
int a = 20;
int b = 7;
System.out.println("Addition: " + (a + b)); // 27
System.out.println("Subtraction: " + (a - b)); // 13
System.out.println("Multiplication: " + (a * b)); // 140
System.out.println("Division: " + (a / b)); // 2 (integer division)
System.out.println("Modulus: " + (a % b)); // 6 (remainder)
// Division with doubles
double x = 20.0;
double y = 7.0;
System.out.println("Double division: " + (x / y)); // 2.857142857142857
Integer Division: int á int = int (ááá áááşáá áşáááş)
int result1 = 10 / 3; // 3 (not 3.333...)
int result2 = 7 / 2; // 3 (not 3.5)
Double Division: double á double = double (ááá ááŤáááş)
double result3 = 10.0 / 3.0; // 3.3333333333333335
double result4 = 7.0 / 2.0; // 3.5
Mixed Division: int áá˛áˇ double ááąáŹáá˛áˇáĄá፠double ááźá áşáá˝áŹá¸áááş
double result5 = 10 / 3.0; // 3.3333333333333335
double result6 = 10.0 / 3; // 3.3333333333333335
Modulus (%) á division áá˛áˇ remainder (ááťááşááááşá¸) ááᯠreturn ááŻááşááŤáááşá
10 % 3 = 1 // 10 á 3 = 3 ááťááş 1
15 % 4 = 3 // 15 á 4 = 3 ááťááş 3
20 % 5 = 0 // 20 á 5 = 4 ááťááş 0 (ááźááşáá˝áŹá¸áááş)
7 % 2 = 1 // 7 á 2 = 3 ááťááş 1 (odd number)
Common Uses:
- Even/Odd check:
number % 2 == 0â Even - Cycle/Loop: Array index wrapping
- Divisibility:
number % 5 == 0â Divisible by 5
3. Unary Operators (áá áşááŻáááşá¸ Operand)
Operators that work with a single operand.
3.1 Increment and Decrement
++ (Increment - áááŻá¸ááźááşá¸)
int x = 5;
// Post-increment (áĄáááş ááŻáśá¸ááźáŽá¸ááž áááŻá¸áááş)
int a = x++; // a = 5, x = 6
System.out.println("a: " + a + ", x: " + x);
// Pre-increment (áĄáááş áááŻá¸ááźáŽá¸ááž ááŻáśá¸áááş)
int y = 5;
int b = ++y; // b = 6, y = 6
System.out.println("b: " + b + ", y: " + y);
-- (Decrement - ááťážáąáŹáˇááźááşá¸)
int x = 10;
// Post-decrement
int a = x--; // a = 10, x = 9
// Pre-decrement
int y = 10;
int b = --y; // b = 9, y = 9
Post-increment (x++): Current value ááŻáśá¸ááźáŽá¸ááž 1 áááŻá¸áááş
int x = 5;
int y = x++; // y gets 5, then x becomes 6
// y = 5, x = 6
Pre-increment (++x): áĄáááş 1 áááŻá¸ááźáŽá¸ááž value ááŻáśá¸áááş
int x = 5;
int y = ++x; // x becomes 6 first, then y gets 6
// y = 6, x = 6
Common Usage: Loops áážáŹ áĄááťáŹá¸ááŻáśá¸ ááŻáśá¸áááş
for (int i = 0; i < 10; i++) {
// i++ increments after each iteration
}
3.2 Unary Plus and Minus
int a = 10;
int b = -a; // Unary minus: b = -10
int c = +a; // Unary plus: c = 10 (no change)
System.out.println("a: " + a); // 10
System.out.println("b: " + b); // -10
System.out.println("c: " + c); // 10
3.3 Logical NOT (!)
boolean isTrue = true;
boolean isFalse = !isTrue; // NOT operator
System.out.println("isTrue: " + isTrue); // true
System.out.println("isFalse: " + isFalse); // false
System.out.println("!isFalse: " + !isFalse); // true
4. Assignment Operators (áááşáážááşááźááşá¸)
| Operator | Example | Same As |
|---|---|---|
= |
x = 5
|
x = 5
|
+= |
x += 3
|
x = x + 3
|
-= |
x -= 3
|
x = x - 3
|
*= |
x *= 3
|
x = x * 3
|
/= |
x /= 3
|
x = x / 3
|
%= |
x %= 3
|
x = x % 3
|
int x = 10;
x += 5; // x = x + 5 â x = 15
System.out.println("After += : " + x);
x -= 3; // x = x - 3 â x = 12
System.out.println("After -= : " + x);
x *= 2; // x = x * 2 â x = 24
System.out.println("After *= : " + x);
x /= 4; // x = x / 4 â x = 6
System.out.println("After /= : " + x);
x %= 5; // x = x % 5 â x = 1
System.out.println("After %= : " + x);
Compound assignment operators áá˝áąá code ááᯠáááŻááąáŹááşá¸áĄáąáŹááş ááąá¸áááŻááşáĄáąáŹááş ááŻááşááąá¸ááŤáááşá
x += 5áx = x + 5áá˛áˇ áĄáá°áá°ááŤ- Typing ááťážáąáŹáˇáááŻááşáááşá code áážááşá¸áááş
- All arithmetic operators áá˛áˇ ááŻáśá¸áááŻáˇááááş
5. Relational Operators (áážááŻááşá¸áážááşááźááşá¸)
Used to compare two values. Returns boolean (true/false).
| Operator | Name | Example | Result |
|---|---|---|---|
== |
Equal to (áá°áááş) | 5 == 5
|
true |
!= |
Not equal (ááá°áááş) | 5 != 3
|
true |
>
|
Greater than (ááźáŽá¸áááş) |
5 > 3
|
true |
<
|
Less than (ááąá¸áááş) |
3 < 5
|
true |
>=
|
Greater or equal (ááźáŽá¸áááş áááŻáˇ áá°áááş) |
5 >= 5
|
true |
<=
|
Less or equal (ááąá¸áááş áááŻáˇ áá°áááş) |
3 <= 5
|
true |
int a = 10;
int b = 5;
System.out.println("a == b: " + (a == b)); // false
System.out.println("a != b: " + (a != b)); // true
System.out.println("a > b: " + (a > b)); // true
System.out.println("a < b: " + (a < b)); // false
System.out.println("a >= b: " + (a >= b)); // true
System.out.println("a <= b: " + (a <= b)); // false
// Common usage in conditions
if (a > b) {
System.out.println("a is greater than b");
}
â ď¸ Common Mistake
// â Wrong: Using = instead of ==
if (x = 5) { // Assignment, not comparison!
// Error: incompatible types
}
// â
Correct: Using == for comparison
if (x == 5) { // Comparison
System.out.println("x is 5");
}
Remember: = is assignment, == is comparison!
6. Logical Operators (ááŻáášáá áááŻááşááŹ)
Used to combine multiple boolean expressions.
| Operator | Name | Description | Example |
|---|---|---|---|
&& |
AND (áážááˇáş) | Both must be true |
true && true = true
|
|| |
OR (áááŻáˇ) | At least one must be true |
true || false = true
|
! |
NOT (áááŻááş) | Reverses the value |
!true = false
|
6.1 AND Operator (&&)
int age = 25;
boolean hasLicense = true;
// Both conditions must be true
if (age >= 18 && hasLicense) {
System.out.println("Can drive");
}
// Truth table for AND
System.out.println(true && true); // true
System.out.println(true && false); // false
System.out.println(false && true); // false
System.out.println(false && false); // false
AND operator (&&) á conditions áĄáŹá¸ááŻáśá¸ true ááźá áşááž true return ááŻááşááŤáááşá
true && true= truetrue && false= falsefalse && true= falsefalse && false= false
Short-circuit: First condition false ááźá áşáááş second ááᯠcheck áááŻááşááąáŹáˇááŤ
6.2 OR Operator (||)
boolean isWeekend = false;
boolean isHoliday = true;
// At least one condition must be true
if (isWeekend || isHoliday) {
System.out.println("No work today!");
}
// Truth table for OR
System.out.println(true || true); // true
System.out.println(true || false); // true
System.out.println(false || true); // true
System.out.println(false || false); // false
OR operator (||) á conditions áá áşááŻáᯠtrue ááźá áşáááş true return ááŻááşááŤáááşá
true || true= truetrue || false= truefalse || true= truefalse || false= false
Short-circuit: First condition true ááźá áşáááş second ááᯠcheck áááŻááşááąáŹáˇááŤ
6.3 NOT Operator (!)
boolean isRaining = false;
// Reverses the boolean value
if (!isRaining) {
System.out.println("Go for a walk!");
}
System.out.println(!true); // false
System.out.println(!false); // true
boolean isReady = true;
System.out.println("Not ready: " + !isReady); // false
7. Ternary Operator (ááŻáśá¸áááŻááşá¸ Operator)
A shorthand for if-else statement.
condition ? valueIfTrue : valueIfFalse;
int age = 20;
// Ternary operator
String status = (age >= 18) ? "Adult" : "Minor";
System.out.println(status); // Adult
// Equivalent if-else
String status2;
if (age >= 18) {
status2 = "Adult";
} else {
status2 = "Minor";
}
// More examples
int a = 10, b = 20;
int max = (a > b) ? a : b;
System.out.println("Max: " + max); // 20
int num = 7;
String type = (num % 2 == 0) ? "Even" : "Odd";
System.out.println(num + " is " + type); // 7 is Odd
Ternary operator (? :) á if-else áá˛áˇ short form ááźá áşááŤáááşá
Format: condition ? value1 : value2
- Condition true ááᯠvalue1 return ááŻááşáááş
- Condition false ááᯠvalue2 return ááŻááşáááş
- Code áááŻáááşá áážááşá¸áááş
- Simple conditions áĄáá˝ááş áááˇáşááąáŹáşáááş
8. Operator Precedence (áŚá¸á áŹá¸ááąá¸ áĄá áŽáĄá ááş)
Operators are evaluated in a specific order when multiple operators appear in an expression.
int result = 10 + 5 * 2;
// Multiplication happens first: 5 * 2 = 10
// Then addition: 10 + 10 = 20
System.out.println(result); // 20
// Use parentheses to change order
int result2 = (10 + 5) * 2;
// Parentheses first: 10 + 5 = 15
// Then multiplication: 15 * 2 = 30
System.out.println(result2); // 30
Precedence Order (High to Low)
- Parentheses:
() - Unary:
++, --, !, +, - - Multiplicative:
*, /, % - Additive:
+, - - Relational:
<, >, <=, >= - Equality:
==, != - Logical AND:
&& - Logical OR:
|| - Ternary:
? : - Assignment:
=, +=, -=, *=, /=, %=
Use parentheses for clarity! Precedence áážááşááááşáááşá parentheses ááŻáśá¸áááş code áážááşá¸ááźáŽá¸ bug áááşá¸áááşá
// Less clear
int x = 10 + 5 * 2;
// More clear
int x = 10 + (5 * 2);
9. Complete Example
public class OperatorDemo {
public static void main(String[] args) {
// Arithmetic operators
int a = 20, b = 10;
System.out.println("=== Arithmetic ===");
System.out.println("a + b = " + (a + b));
System.out.println("a - b = " + (a - b));
System.out.println("a * b = " + (a * b));
System.out.println("a / b = " + (a / b));
System.out.println("a % b = " + (a % b));
// Unary operators
System.out.println("\n=== Unary ===");
int x = 5;
System.out.println("x++ = " + (x++)); // 5, then x=6
System.out.println("x = " + x); // 6
System.out.println("++x = " + (++x)); // 7
System.out.println("x = " + x); // 7
// Relational operators
System.out.println("\n=== Relational ===");
System.out.println("a > b: " + (a > b));
System.out.println("a == b: " + (a == b));
System.out.println("a != b: " + (a != b));
// Logical operators
System.out.println("\n=== Logical ===");
boolean p = true, q = false;
System.out.println("p && q: " + (p && q));
System.out.println("p || q: " + (p || q));
System.out.println("!p: " + (!p));
// Ternary operator
System.out.println("\n=== Ternary ===");
int age = 20;
String status = (age >= 18) ? "Adult" : "Minor";
System.out.println("Status: " + status);
// Assignment operators
System.out.println("\n=== Assignment ===");
int y = 10;
y += 5; // y = y + 5
System.out.println("After += 5: " + y);
y *= 2; // y = y * 2
System.out.println("After *= 2: " + y);
}
}
10. Key Takeaways
- Arithmetic: +, -, *, /, % (modulus = remainder)
- Unary: ++, -- (increment/decrement), !, +, -
- Assignment: =, +=, -=, *=, /=, %=
- Relational: ==, !=, >, <, >=, <= (returns boolean)
- Logical: && (AND), || (OR), ! (NOT)
- Ternary: condition ? value1 : value2
- Post-increment (x++): Use then increment
- Pre-increment (++x): Increment then use
- Integer division: 10 / 3 = 3 (no decimal)
- Modulus: 10 % 3 = 1 (remainder)
- = vs ==: Assignment vs Comparison
- Use parentheses for clarity in complex expressions
đĄ Practice Exercise
Create a Calculator program:
- Use all arithmetic operators (+, -, *, /, %)
- Demonstrate pre/post increment
- Use relational operators to compare results
- Use logical operators to combine conditions
- Use ternary operator for output messages
đĄ Quick Quiz
Q1: What is 10 % 3?
Q2: What does x++ do?
Q3: Which operator is used for comparison?