Lesson 7 of 24 Java Operators
Lesson 7 ⏱ 15 min read

Java Operators

Java မှာ calculations တွေ လုပ်တဲ့ operators များ၊ arithmetic, relational, logical, assignment operators တွေကို အသေးစိတ် လေ့လာကြမယ်။

1. Operators ဆိုတာ ဘာလဲ?

An operator is a symbol that performs operations on variables and values.

Simple Operator Example
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
⚡ Operators အကြောင်း

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
Arithmetic Examples
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
➗ Division အကြောင်း

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 Operator အကြောင်း

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
🔄 Increment/Decrement အကြောင်း

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

Unary Examples
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 (!)

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
Assignment Examples
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);
📝 Assignment Operators မြန်မာလို

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
Relational Examples
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 (&&)

AND Examples
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 (&&) အကြောင်း

AND operator (&&) က conditions အားလုံး true ဖြစ်မှ true return လုပ်ပါတယ်။

  • true && true = true
  • true && false = false
  • false && true = false
  • false && false = false

Short-circuit: First condition false ဖြစ်ရင် second ကို check မလုပ်တော့ပါ

6.2 OR Operator (||)

OR Examples
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 (||) အကြောင်း

OR operator (||) က conditions တစ်ခုခု true ဖြစ်ရင် true return လုပ်ပါတယ်။

  • true || true = true
  • true || false = true
  • false || true = true
  • false || false = false

Short-circuit: First condition true ဖြစ်ရင် second ကို check မလုပ်တော့ပါ

6.3 NOT Operator (!)

NOT Examples
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.

Ternary Syntax
condition ? valueIfTrue : valueIfFalse;
Ternary Examples
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 အကြောင်း

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.

Precedence Example
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)

  1. Parentheses: ()
  2. Unary: ++, --, !, +, -
  3. Multiplicative: *, /, %
  4. Additive: +, -
  5. Relational: <, >, <=, >=
  6. Equality: ==, !=
  7. Logical AND: &&
  8. Logical OR: ||
  9. Ternary: ? :
  10. Assignment: =, +=, -=, *=, /=, %=
🎯 Best Practice

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

OperatorDemo.java
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?

← Previous
Next →