Variables & Data Types (Variable နဲ့ Data Type များ)

B2.1.1 - Construct and trace programs using a range of global and local variables of various data types.
မြန်မာလို: data type (ဒေတာ အမျိုးအစား) အမျိုးမျိုး ရှိတဲ့ global variable (class တစ်ခုလုံးက သုံးလို့ရတဲ့ variable) တွေ၊ local variable (method ထဲမှာပဲ ရှိတဲ့ variable) တွေကို သုံးတဲ့ program တွေကို ကိုယ်တိုင် တည်ဆောက်နိုင်ပြီး trace (တစ်ဆင့်ချင်း လိုက်ပြီး မှတ်) လုပ်နိုင်ရပါမယ်။


အဓိက အယူအဆများ

Variable ဆိုတာ ဒေတာ သိမ်းထားဖို့ နာမည်ပေးထားတဲ့ နေရာ (container) တစ်ခုပါ။ Java မှာ variable တစ်ခုကို မသုံးခင် သူ့ရဲ့ type (အမျိုးအစား) ကို အရင် ကြေညာ (declare) ထားရပါတယ်။

Declaration (ကြေညာခြင်း) နဲ့ Initialisation (ပထမဆုံး တန်ဖိုး ထည့်ခြင်း)

int age;           // declared - memory reserved, no value yet
age = 16;          // initialised - value assigned

int lives = 3;     // declared and initialised in one line
# Python has no separate declaration step - a variable exists
# once you assign it, and its type comes from the value.
age = 16       # an int, because 16 is a whole number

lives = 3

Java code ရဲ့ ပထမ line က variable ကို declare လုပ်ရုံပဲ လုပ်ပါတယ်: memory (မှတ်ဉာဏ်) ထဲမှာ နေရာ ယူထားပြီး တန်ဖိုး မရှိသေးပါဘူး။ ဒုတိယ line က တန်ဖိုး ထည့်ပြီး initialise လုပ်ပါတယ်။ တတိယ line က declare နဲ့ initialise ကို တစ်ကြောင်းတည်းမှာ လုပ်ပါတယ်။ (Python မှာတော့ declare လုပ်တဲ့ အဆင့် သီးခြား မရှိပါဘူး။ တန်ဖိုး ထည့်လိုက်တာနဲ့ variable ဖြစ်လာပြီး သူ့ type က တန်ဖိုးကနေ လာပါတယ်၊ ဥပမာ 16 က ကိန်းပြည့် ဖြစ်လို့ int ပါ။)

Constant (ပြောင်းလို့ မရတဲ့ တန်ဖိုး) များ

တန်ဖိုး ထည့်ပြီးနောက် ဘယ်တော့မှ မပြောင်းသင့်တဲ့ တန်ဖိုးတွေအတွက် final ကို သုံးပါ:

final double PI = 3.14159;
final int MAX_ATTEMPTS = 3;
# Python has no final keyword - ALL_CAPS names signal "treat as a constant"
PI = 3.14159
MAX_ATTEMPTS = 3

(Python မှာ final keyword (programming language မှာ အထူး အဓိပ္ပာယ်ရှိတဲ့ စကားလုံး) မရှိပါဘူး။ နာမည်ကို စာလုံးကြီး အားလုံးနဲ့ ရေးတာက “ဒါကို constant အဖြစ် သဘောထားပါ” လို့ ပြတဲ့ အမှတ်အသားပါ။)

ထုံးစံအရ constant တွေကို UPPER_SNAKE_CASE (စာလုံးကြီးတွေနဲ့ ရေးပြီး စကားလုံးတွေကြားမှာ underscore _ ခံတဲ့ ပုံစံ) နဲ့ ရေးကြပါတယ်။ final variable ကို တန်ဖိုး ပြန်ထည့်ဖို့ ကြိုးစားရင် compiler (code တစ်ခုလုံးကို ကြိုတင် ဘာသာပြန်တဲ့ program) က error ပြပါလိမ့်မယ်။


Data Type (ဒေတာ အမျိုးအစား) များ

Data type က variable တစ်ခု ဘယ်လို တန်ဖိုးမျိုး သိမ်းမလဲ၊ memory ဘယ်လောက် ယူထားရမလဲဆိုတာကို Java ကို ပြောပြပါတယ်။

Primitive Type (Java ရဲ့ အခြေခံ ဒေတာ အမျိုးအစား) များ: IB မှာ စစ်တဲ့ type တွေ

Type အရွယ်အစား သိမ်းတဲ့ တန်ဖိုး ဥပမာ
int 32 bits Integer (ကိန်းပြည့်) int score = 42;
double 64 bits ဒသမ ကိန်း double price = 9.99;
boolean - true ဒါမှမဟုတ် false ပဲ boolean passed = true;
char 16 bits Unicode (ကမ္ဘာ့ စာလုံး အားလုံးအတွက် code စနစ်) character (စာလုံး ဒါမှမဟုတ် သင်္ကေတ) တစ်လုံး char grade = 'A';

(Python မှာတော့ type က တန်ဖိုးကနေ လာပါတယ်: x = 5 ဆိုရင် int၊ x = 9.99 ဆိုရင် float၊ True/False ဆိုရင် boolean ဖြစ်ပါတယ်။ char မရှိလို့ "A" လို character တစ်လုံးတည်းပါတဲ့ string ကို အစားသုံးပါတယ်။)

char က single quote ('A') ကို သုံးပြီး String က double quote ("Alice") ကို သုံးပါတယ်။ နှစ်ခု ရောသုံးရင် compile error (compile လုပ်ချိန်မှာ ဖြစ်တဲ့ error) တက်ပါတယ်။

String (စာသား)

String က primitive type မဟုတ်ပါဘူး၊ object (class တစ်ခုကနေ ဖန်တီးထားတဲ့ instance) တစ်ခုပါ။ ဒါက နောက်ပိုင်း OOP (object တွေကို အခြေခံပြီး program ရေးနည်း) မှာ အရေးကြီးလာပါမယ်။ အခုအတွက်တော့:

String name = "Alice";                        // double quotes, capital S
String greeting = "Hello, " + name;           // "Hello, Alice"
String empty = "";                            // empty string, not null
name = "Alice"                       # no type keyword - the quotes make it a string
greeting = "Hello, " + name          # "Hello, Alice"
empty = ""                           # empty string, not None

Java မှာ String ကို S အကြီးနဲ့ ရေးပြီး တန်ဖိုးကို double quote ထဲမှာ ထည့်ပါတယ်။ "" က စာလုံး မပါတဲ့ empty string ဖြစ်ပြီး null (ဘာ object ကိုမှ မညွှန်းတဲ့ တန်ဖိုး) မဟုတ်ပါဘူး။ (Python မှာလည်း "" က None မဟုတ်ပါဘူး။)

String တွေကို .equals() နဲ့ နှိုင်းယှဉ်ပါ၊ == နဲ့ ဘယ်တော့မှ မနှိုင်းယှဉ်ပါနဲ့:

if (name.equals("Alice")) { ... }   // correct
if (name == "Alice") { ... }        // WRONG - compares memory addresses

ပထမ line က မှန်ပါတယ်။ ဒုတိယ line က မှားပါတယ်၊ == က စာသားကို မဟုတ်ဘဲ memory address (memory ထဲက နေရာ လိပ်စာ) တွေကို နှိုင်းယှဉ်လို့ပါ။ (Python မှာတော့ == က မှန်ကန်တဲ့ နည်းပါ: စာသားရဲ့ အကြောင်းအရာကို နှိုင်းယှဉ်ပြီး .equals() မရှိပါဘူး။)


နာမည်ပေးတဲ့ ထုံးစံများ

ဘာအတွက်လဲ စည်းမျဉ်း ဥပမာ
Variable နဲ့ method (သီးခြား အလုပ်တစ်ခု လုပ်ပေးတဲ့ code အပိုင်း) စာလုံးအသေးနဲ့ စပါ၊ camelCase (ပထမ စကားလုံး အသေး၊ နောက် စကားလုံးတွေကို စာလုံးကြီးနဲ့ စ) totalScore, playerName
Class (object တွေ ဖန်တီးဖို့ ပုံစံခွက်) စာလုံးကြီးနဲ့ စပါ၊ PascalCase (စကားလုံးတိုင်းကို စာလုံးကြီးနဲ့ စ) GradeCalculator
Constant စာလုံးကြီး အားလုံး၊ စကားလုံးကြားမှာ underscore MAX_ATTEMPTS, TAX_RATE

Operator (တန်ဖိုးတွေကို တွက်ချက်တဲ့ သင်္ကေတ) များ

Arithmetic Operator (ဂဏန်း တွက်ချက်တဲ့ operator) များ

Operator အဓိပ္ပာယ် ဥပမာ ရလဒ်
+ အပေါင်း 5 + 3 8
- အနုတ် 5 - 3 2
* အမြှောက် 5 * 3 15
/ အစား 7 / 2 3 (ကိန်းပြည့်!)
% Modulo (အကြွင်း) 7 % 2 1

Integer division (ကိန်းပြည့် အစား) ထောင်ချောက်: Operand (တွက်ချက်မှုထဲက တန်ဖိုး) နှစ်ခုလုံး int ဖြစ်နေရင် Java က ဒသမ အပိုင်းကို ဖြုတ်ပစ်ပါတယ်။ 7 / 2 က 3.5 မဟုတ်ဘဲ 3 ကို ပေးပါတယ်။ ဒသမ ရလဒ် လိုချင်ရင် operand တစ်ခုကို cast (type ပြောင်း) လုပ်ပါ: (double) 7 / 2 က 3.5 ကို ပေးပါတယ်။ (Python မှာတော့ / က ဒသမ အဖြေကို အမြဲ ပေးပါတယ်: 7 / 2 က 3.5 ပါ။ ကိန်းပြည့် အစား လိုချင်ရင် // ကို သုံးပါ: 7 // 2 က 3 ပါ။ အနုတ်ကိန်းတွေမှာတော့ နှစ်ခု မတူပါဘူး: Python ရဲ့ // က ငယ်ဘက်က ကိန်းပြည့်ဆီ ယူလို့ (round down) -7 // 2 က -4 ဖြစ်ပြီး Java ရဲ့ -7 / 2 ကတော့ ဒသမ အပိုင်းကို ဖြုတ်လိုက်လို့ -3 ဖြစ်ပါတယ်။)

String Concatenation (String တွေကို ဆက်ခြင်း)

+ operator က String တွေကို ဆက်ပေးပြီး ဂဏန်းတွေကိုလည်း စာသားအဖြစ် အလိုအလျောက် ပြောင်းပေးပါတယ်:

String name = "Alice";
int age = 16;
System.out.println("Name: " + name + ", Age: " + age);
// Output: Name: Alice, Age: 16
name = "Alice"
age = 16
# Python will not join a number onto text with + - convert it with str() first
print("Name: " + name + ", Age: " + str(age))
# Output: Name: Alice, Age: 16

တွက်တဲ့ အစီအစဉ်ကို သတိထားပါ: "A" + 1 + 2 က "A12" ကို ပေးပါတယ် (ဘယ်ကနေ ညာကို တစ်ခုချင်း ဆက်သွားလို့ပါ)၊ ဒါပေမယ့် 1 + 2 + "A" က "3A" ကို ပေးပါတယ် (အရင် ပေါင်းပြီးမှ ဆက်လို့ပါ)။ (Python ကတော့ လုံးဝ လက်မခံပါဘူး: "A" + 1 က TypeError (type မကိုက်လို့ ဖြစ်တဲ့ error) ပါ၊ str() နဲ့ စာသားအဖြစ် အရင် ပြောင်းပါ။)


Type Casting (data type တစ်ခုကနေ တစ်ခုကို ပြောင်းခြင်း)

တစ်ခါတစ်လေ တန်ဖိုး တစ်ခုကို type တစ်ခုကနေ နောက် type တစ်ခုကို ပြောင်းဖို့ လိုပါတယ်။

Implicit (Widening): အလိုအလျောက် ပြောင်းခြင်း

Java က type အသေးကို type အကြီးအဖြစ် အလိုအလျောက် ပြောင်းပေးပါတယ် (widening ဆိုတာ ပိုကျယ်တဲ့ type ကို ပြောင်းတာပါ):

int total = 365;
double average = total;          // int automatically widened to double
total = 365
average = float(total)   # average = total would stay an int; float() gives 365.0 like Java

ဒီမှာ int တန်ဖိုး 365 က double အဖြစ် အလိုအလျောက် ပြောင်းသွားပါတယ်။ (Python မှာ average = total လို့ ရေးရင် int အတိုင်းပဲ ရှိနေလို့ Java လို 365.0 ရဖို့ float() ကို သုံးပါတယ်။)

Explicit (Narrowing): ကိုယ်တိုင် ပြောင်းခြင်း

Type အသေးကို ပြောင်းဖို့ (narrowing) cast ကို ကိုယ်တိုင် ရေးပေးရပါတယ်။ အချက်အလက် တချို့ ပျောက်သွားနိုင်ပါတယ်:

double price = 9.99;
int truncated = (int) price;     // 9 - decimal part dropped, NOT rounded

char letter = 'A';
int code = (int) letter;         // 65 (Unicode value)

int code2 = 66;
char letter2 = (char) code2;     // 'B'
price = 9.99
truncated = int(price)     # 9 - decimal part dropped, NOT rounded

letter = "A"               # Python has no char type - a one-character string
code = ord(letter)         # 65 (Unicode value)

code2 = 66
letter2 = chr(code2)       # "B"

(int) price က 9 ကို ပေးပါတယ်: ဒသမ အပိုင်းကို ဖြုတ်ပစ်တာပါ၊ round (အနီးဆုံး ကိန်းပြည့်ကို ယူ) လုပ်တာ မဟုတ်ပါဘူး။ (int) letter က 'A' ရဲ့ Unicode တန်ဖိုး 65 ကို ပေးပြီး (char) code2 က 66 ကို 'B' အဖြစ် ပြောင်းပေးပါတယ်။ (Python မှာ char type မရှိလို့ ord() နဲ့ chr() ကို သုံးပါတယ်။)

(int) 3.9 က 4 မဟုတ်ဘဲ 3 ကို ပေးပါတယ်။ int ကို cast လုပ်ရင် truncate (ဒသမ အပိုင်းကို ဖြတ်ပစ်) လုပ်တာပါ၊ round လုပ်တာ မဟုတ်ပါဘူး။ (Python ရဲ့ int() ကလည်း အတူတူပဲ truncate လုပ်ပါတယ်: int(3.9) က 3 ပါ။)


Scanner နဲ့ user ဆီက input ယူခြင်း

Scanner class (keyboard ဒါမှမဟုတ် file ကနေ input ဖတ်တဲ့ Java class) က user ရိုက်ထည့်တဲ့ input (program ထဲ ထည့်ပေးတဲ့ ဒေတာ) ကို ဖတ်ပေးပါတယ်။ သူ့ကို သုံးဖို့ import statement (တခြားနေရာက class ကို ယူသုံးမယ်လို့ ကြေညာတဲ့ code line) လိုပါတယ်။

import java.util.Scanner;

public class Greeting {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter your name: ");
        String name = input.nextLine();

        System.out.print("Enter your age: ");
        int age = input.nextInt();

        System.out.println("Hello, " + name + "! You are " + age + " years old.");
    }
}
# Python needs no import or Scanner - input() reads a whole line as text
name = input("Enter your name: ")
age = int(input("Enter your age: "))     # wrap input() in int() to get a number

print("Hello, " + name + "! You are " + str(age) + " years old.")

အသုံးများတဲ့ Scanner method များ

Method ဖတ်တဲ့ အရာ
nextInt() နောက်ထပ် ကိန်းပြည့် တစ်ခု
nextDouble() နောက်ထပ် ဒသမ ကိန်း တစ်ခု
next() နောက်ထပ် စကားလုံး တစ်လုံး (whitespace (space၊ tab စတဲ့ ကွက်လပ်) တွေ့ရင် ရပ်ပါတယ်)
nextLine() Space တွေ အပါအဝင် line တစ်ကြောင်းလုံး

(Python မှာတော့ function (method လိုပဲ အလုပ်တစ်ခု လုပ်ပေးတဲ့ code အပိုင်း) တစ်ခုတည်းက ဒါတွေ အားလုံးကို လုပ်ပေးပါတယ်: input() က line တစ်ကြောင်းကို စာသားအဖြစ် ဖတ်ပါတယ်။ ဂဏန်း လိုချင်ရင် int(...) ဒါမှမဟုတ် float(...) ထဲ ထည့်ပါ။)

nextLine() ထောင်ချောက်: nextInt() ဒါမှမဟုတ် nextDouble() ကို ခေါ်ပြီးတဲ့အခါ newline character \n (line အသစ်ကို ပြတဲ့ character) က buffer (ဖတ်ဖို့ ကျန်နေတဲ့ input ကို ခဏ သိမ်းထားတဲ့ နေရာ) ထဲမှာ ကျန်နေပါတယ်။ နောက်က လိုက်လာတဲ့ nextLine() က အဲဒီ ကျန်နေတဲ့ newline ကို ဖတ်ပြီး empty string (စာလုံး မပါတဲ့ string) အဖြစ် ယူပါလိမ့်မယ်။ ပြင်နည်း: နှစ်ခုကြားမှာ ရလဒ်ကို မသုံးဘဲ ပစ်လိုက်မယ့် input.nextLine() တစ်ကြောင်း ထပ်ထည့်ပါ။


ဖြေရှင်းပြထားတဲ့ ဥပမာများ

ဥပမာ 1: စက်ဝိုင်းရဲ့ ဧရိယာ

import java.util.Scanner;

public class AreaOfCircle {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        final double PI = 3.14159;

        System.out.print("Enter the radius: ");
        double radius = input.nextDouble();

        double area = PI * radius * radius;

        System.out.println("Area = " + area);
    }
}
PI = 3.14159   # no final keyword - ALL_CAPS marks it as a constant

radius = float(input("Enter the radius: "))

area = PI * radius * radius

print("Area =", area)

Input: 5.0 → Output (program က ထုတ်ပြတဲ့ ရလဒ်): Area = 78.53975


ဥပမာ 2: အပူချိန် ပြောင်းပေးတဲ့ program

import java.util.Scanner;

public class TempConverter {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter temperature in Celsius: ");
        double celsius = input.nextDouble();

        double fahrenheit = celsius * 9.0 / 5.0 + 32;

        System.out.println(celsius + "°C = " + fahrenheit + "°F");
    }
}
celsius = float(input("Enter temperature in Celsius: "))

fahrenheit = celsius * 9.0 / 5.0 + 32

print(str(celsius) + "°C = " + str(fahrenheit) + "°F")

Input: 100 → Output: 100.0°C = 212.0°F

9 / 5 အစား 9.0 / 5.0 လို့ ရေးထားတာကို သတိပြုပါ။ celsius * (9 / 5) လို့ ရေးရင် celsius * 1 ဖြစ်သွားပါလိမ့်မယ်၊ ဘာလို့လဲဆိုတော့ 9 / 5 သက်သက်က integer division ဖြစ်လို့ပါ။ 9.0 / 5.0 ကို သုံးရင် expression (တွက်ချက်မှု ဖော်ပြချက်) ကို ဘယ်အစီအစဉ်နဲ့ ရေးရေး ရလဒ် မှန်နေပါတယ်။ (Python မှာတော့ 9 / 5 က 1.8 ဖြစ်ပြီးသားပါ၊ ဒီ ထောင်ချောက်က Java မှာပဲ ရှိပါတယ်။)


ဥပမာ 3: Variable နှစ်ခုကို swap (တန်ဖိုး နှစ်ခု နေရာချင်း လဲခြင်း) လုပ်ခြင်း

int a = 10;
int b = 20;

int temp = a;    // save a's value
a = b;           // overwrite a with b
b = temp;        // put original a into b

System.out.println("a = " + a);   // a = 20
System.out.println("b = " + b);   // b = 10
a = 10
b = 20

temp = a    # save a's value
a = b       # overwrite a with b
b = temp    # put original a into b

print("a =", a)   # a = 20
print("b =", b)   # b = 10

temp က a ရဲ့ တန်ဖိုးကို သိမ်းထားပါတယ်၊ ပြီးတော့ a ထဲကို b ရဲ့ တန်ဖိုးကို ထည့်ပါတယ်၊ နောက်ဆုံးမှာ b ထဲကို မူလ a တန်ဖိုး (temp) ကို ထည့်ပါတယ်။

အဓိက သိထားရမှာ: ရိုးရိုး assignment (variable ထဲ တန်ဖိုး ထည့်ခြင်း) တွေနဲ့ swap လုပ်ရင် temporary variable (တန်ဖိုးကို ခဏ သိမ်းထားတဲ့ variable) တစ်ခု လိုပါတယ်။ Variable တစ်ခုကို နောက်တစ်ခုထဲ အရင် ထည့်လိုက်ရင် သူ့ရဲ့ မူလ တန်ဖိုး ပျောက်သွားလို့ပါ။ (Python မှာ a, b = b, a လို့လည်း ရေးလို့ရပါတယ်။)


ဥပမာ 4: Integer division နဲ့ modulo

int total = 17;
int groupSize = 5;

int fullGroups = total / groupSize;   // 3
int remaining = total % groupSize;     // 2

System.out.println("Full groups: " + fullGroups);
System.out.println("Remaining: " + remaining);
total = 17
group_size = 5

full_groups = total // group_size   # 3 - // is whole-number division (Java's int /)
remaining = total % group_size      # 2

print("Full groups:", full_groups)
print("Remaining:", remaining)

% ကို သုံးတဲ့ နေရာများ:

  • စုံကိန်း ဟုတ်မဟုတ် စစ်ခြင်း: n % 2 == 0
  • နောက်ဆုံး ဂဏန်းလုံးကို ထုတ်ယူခြင်း: n % 10
  • အဆုံးရောက်ရင် အစကို ပြန်လှည့်ခြင်း (နာရီ၊ array (အရွယ်အစား ပုံသေ၊ အမျိုးအစားတူ ဒေတာစု) ရဲ့ index (နေရာအမှတ်)): index % length

Quick Check (အမြန် စစ်ဆေးမေးခွန်း)

Q1. Java မှာ 7 / 2 ရဲ့ ရလဒ်က ဘာလဲ။

Q2. 'A' လို character တစ်လုံးတည်းကို ဘယ် data type က သိမ်းသလဲ။

Q3. (int) 3.9 ရဲ့ တန်ဖိုးက ဘာလဲ။

Q4. "Score: " + 42 က ဘာကို ထုတ်ပေးသလဲ။

Q5. 17 % 5 ရဲ့ ရလဒ်က ဘာလဲ။

Q6. final keyword က variable ကို ဘာလုပ်ပေးသလဲ။


Trace Exercise (code ကို တစ်ဆင့်ချင်း လိုက်ကြည့်ခြင်း)

ဒီ code ကို trace လုပ်ပါ။ Line တစ်ကြောင်း run ပြီးတိုင်း variable တစ်ခုချင်းစီရဲ့ တန်ဖိုးကို ဖြည့်ပါ။

int a = 10;
int b = 4;
int c = a / b;
int d = a % b;
double e = (double) a / b;
a = a + d;
a = 10
b = 4
c = a // b     # Java's whole-number division - Python needs //
d = a % b
e = a / b      # / always gives the decimal answer - no cast needed
a = a + d
Lineabcde
int a = 10 - - - -
int b = 4 10 - - -
int c = a / b 10 4 - -
int d = a % b 10 4 2 -
double e = (double) a / b 10 4 2 2
a = a + d 4 2 2 2.5

Code Completion (code ကွက်လပ် ဖြည့်ခြင်း)

ထောင့်မှန်စတုဂံ တစ်ခုရဲ့ ဧရိယာကို တွက်တဲ့ ဒီ program မှာ ပျောက်နေတဲ့ အပိုင်းတွေကို ဖြည့်ပါ။ Program က Scanner ကို သုံးပြီး user ဆီကနေ အနံ (width) နဲ့ အမြင့် (height) ကို ဖတ်ပါတယ်။ ပြီးရင် မှန်ကန်တဲ့ data type နဲ့ method call (method ကို ခေါ်သုံးခြင်း) ကို သုံးပြီး ဧရိယာကို တွက်ပါတယ်။

import java.util.Scanner;

Scanner input = new Scanner(System.in);
System.out.print("Enter width: ");
 width = input.nextDouble();
System.out.print("Enter height: ");
double height = input.;

double area = ;
System.out.println("Area = " + area);

Temporary variable တစ်ခုကို သုံးပြီး variable နှစ်ခုကို swap လုပ်တဲ့ code ကို ဖြည့်ပါ။ Swap လုပ်ဖို့ တတိယ variable တစ်ခု လိုပါတယ်၊ တန်ဖိုး ပြန်ထည့်တဲ့အခါ တန်ဖိုး တစ်ခု မပျောက်သွားအောင် အဲဒီ variable ထဲမှာ ခဏ သိမ်းထားရလို့ပါ။

int x = 5;
int y = 10;

int temp = ;
x = ;
y = ;

System.out.println("x = " + x);   // should print 10
System.out.println("y = " + y);   // should print 5

Spot the Error (အမှား ရှာခြင်း)

ဒီ code က စာမေးပွဲ အမှတ် သုံးခုရဲ့ ပျမ်းမျှကို တွက်ရမှာပါ။ ဒါပေမယ့် operator precedence (ဘယ် operator ကို အရင် တွက်မလဲ ဆိုတဲ့ စည်းမျဉ်း) နဲ့ integer division ကြောင့် အဖြေ မှားထွက်ပါတယ်။ Bug (code ထဲက အမှား) ကို ရှာပါ။

int score1 = 80;
int score2 = 90;
int score3 = 85;
double average = score1 + score2 + score3 / 3;
System.out.println("Average: " + average);

ဘာမှားနေသလဲ၊ ဘယ်လို ပြင်မလဲ။


ဒီ code က Celsius ကို Fahrenheit အဖြစ် ပြောင်းဖို့ F = 9/5 * C + 32 ဆိုတဲ့ ပုံသေနည်းကို သုံးပါတယ်။ ဒါပေမယ့် integer division ကြောင့် ရလဒ် မှားထွက်ပါတယ်။ Bug ကို ရှာပါ။

int celsius = 100;
double fahrenheit = 9 / 5 * celsius + 32;
System.out.println(celsius + "C = " + fahrenheit + "F");

ဘာမှားနေသလဲ၊ ဘယ်လို ပြင်မလဲ။


Predict the Output (output ကို ကြိုခန့်မှန်းခြင်း)

ဒီ code က ဘာကို print ထုတ်မလဲ။ ဒီ code က integer division (/)၊ modulo operator (%) နဲ့ ဒသမ ရလဒ်ရဖို့ double ကို explicit type casting လုပ်တာတို့ရဲ့ ကွာခြားချက်ကို ပြပါတယ်။ Output ကို အတိအကျ ရိုက်ထည့်ပါ။

int x = 5;
int y = 2;
System.out.println(x / y);
System.out.println(x % y);
System.out.println((double) x / y);
x = 5
y = 2
print(x // y)     # // matches Java's whole-number division
print(x % y)
print(x / y)      # / gives the decimal answer - Java needed the (double) cast
ရရမယ့် output: 2
1
2.5

ဒီ code က ဘာကို print ထုတ်မလဲ။ Java က + ကို ဘယ်ကနေ ညာကို ဘယ်လို တွက်လဲဆိုတာ ဒီ code က ပြပါတယ်: String က အရင် ပေါ်လာရင် နောက်က ဂဏန်းတွေကို စာသားအဖြစ် ဆက်ပါတယ်၊ ဒါပေမယ့် ဂဏန်းတွေက အရင် ပေါ်လာရင် String တစ်ခု မတွေ့မချင်း ဂဏန်းအဖြစ် ပေါင်းပါတယ်။

String s = "Hello";
System.out.println(s + " " + 3 + 4);
System.out.println(3 + 4 + " " + s);
s = "Hello"
# Python never mixes text and numbers with + ("Hello" + 3 is a TypeError),
# so Java's left-to-right rule has no Python twin - convert with str() instead.
print(s + " " + str(3) + str(4))
print(str(3 + 4) + " " + s)
ရရမယ့် output: Hello 34
7 Hello

Practice Exercises (လေ့ကျင့်ခန်းများ)

Core (အခြေခံ အဆင့်)

  1. နာမည်နဲ့ ဂဏန်း။ User ရဲ့ နာမည်နဲ့ အကြိုက်ဆုံး ဂဏန်းကို ဖတ်ပြီး "Hi Alice, your favourite number is 7." လို စာကြောင်း တစ်ကြောင်းကို print ထုတ်တဲ့ program ကို ရေးပါ။

  2. ပျမ်းမျှ တွက်တဲ့ program။ User ဆီကနေ စာမေးပွဲ အမှတ် သုံးခုကို ဖတ်ပြီး ပျမ်းမျှကို ဒသမ ကိန်းအဖြစ် print ထုတ်ပါ။

  3. အကြွေ ပြောင်းတဲ့ program။ User ဆီကနေ cent အရေအတွက်ကို ဖတ်ပြီး အဲဒါက dollar ဘယ်လောက်နဲ့ cent ဘယ်လောက် ဖြစ်လဲဆိုတာ output ထုတ်ပါ (100 cent = 1 dollar)။ Integer division နဲ့ modulo ကို သုံးပါ။

  4. ထောင့်မှန်စတုဂံ တွက်တဲ့ program။ ထောင့်မှန်စတုဂံ တစ်ခုရဲ့ အနံနဲ့ အမြင့်ကို ဖတ်ပါ။ သူ့ရဲ့ ဧရိယာနဲ့ ပတ်လည်အနား အလျား (perimeter) ကို print ထုတ်ပါ။

Extension (တိုးချဲ့ အဆင့်)

  1. အချိန် ပြောင်းတဲ့ program။ စုစုပေါင်း စက္ကန့် အရေအတွက်ကို ဖတ်ပြီး နာရီ၊ မိနစ်နဲ့ စက္ကန့် အဖြစ် ပြောင်းပါ။ ဥပမာ: 3661 စက္ကန့် = 1 နာရီ၊ 1 မိနစ်၊ 1 စက္ကန့်။

  2. String ကို လေ့လာတဲ့ program။ User ဆီကနေ စကားလုံး တစ်လုံးကို ဖတ်ပါ။ သူ့ရဲ့ အရှည် (character အရေအတွက်)၊ ပထမ character၊ နောက်ဆုံး character နဲ့ စကားလုံးကို စာလုံးကြီး (uppercase) နဲ့ print ထုတ်ပါ။

  3. ဂဏန်းတွေ ခွဲထုတ်တဲ့ program။ ဂဏန်း 3 လုံးပါတဲ့ ကိန်း တစ်ခု (ဥပမာ 472) ကို ဖတ်ပြီး / 10 နဲ့ % 10 ကို သုံးကာ ရာဂဏန်း၊ ဆယ်ဂဏန်းနဲ့ ခုဂဏန်းတွေကို ခွဲထုတ်ပါ။

Challenge (စိန်ခေါ်မှု အဆင့်)

  1. BMI တွက်တဲ့ program။ User ဆီကနေ ကိုယ်အလေးချိန် (kg) နဲ့ အရပ် (m) ကို ဖတ်ပါ။ BMI (body mass index၊ ကိုယ်အလေးချိန် ညွှန်းကိန်း) (weight / height²) ကို တွက်ပြီး သင့်တော်တဲ့ ပုံစံနဲ့ ပြပါ။

  2. Round လုပ်ပြီး letter grade ပေးခြင်း။ ဒသမ ပါတဲ့ အမှတ်ကို ဖတ်ပြီး type casting ကို သုံးကာ အနီးဆုံး ကိန်းပြည့်ကို round လုပ်ပါ (အကြံပြုချက်: cast မလုပ်ခင် 0.5 ပေါင်းပါ)။ ပြီးရင် letter grade (A၊ B၊ C လို အက္ခရာနဲ့ ပေးတဲ့ အဆင့်) တစ်ခု သတ်မှတ်ပါ။


GitHub Classroom (အလိုအလျောက် စစ်ဆေးတဲ့ test တွေပါတဲ့ programming assignment များ)

Variables and Data Types
Practice declaring variables, using operators, reading input, and type casting.

A class assignment on GitHub Classroom: use it when your teacher has set it. It needs a free GitHub account.

Working in the Codespace

  1. Accept and open. Accept the assignment above, open the link in the email from GitHub, then press Open in Codespace. In Java, change the status bar from Lightweight to Standard mode.
  2. Write and check. Open src/VariablesAndTypes.java and complete each TODO. Press Run and compare the output with the comments, or run bash run-tests.sh in the Terminal.
  3. Submit. In Source Control, write a message, press Commit, then Sync Changes. Your teacher’s feedback arrives as a pull request called Feedback.

The full Codespace guide, with pictures


ဆက်စပ် ခေါင်းစဉ်များ

အရင် သိထားရမှာများ:

  • မရှိပါ၊ ဒီစာမျက်နှာက Java programming ရဲ့ စမှတ်ပါ

ဆက်စပ် ခေါင်းစဉ်များ:

  • String Methods (String နဲ့ သုံးတဲ့ method များ): စာသားကို ကိုင်တွယ်ဖို့ Java မှာ အသင့်ပါတဲ့ method တွေ
  • Exception Handling: runtime error (program run နေစဉ် ဖြစ်တဲ့ error) တွေကို ကိုင်တွယ်ခြင်း
  • Debugging: အမှားတွေကို ရှာပြီး ပြင်ခြင်း

နောက်ထပ် လေ့လာမယ့် ခေါင်းစဉ်များ:

  • Selection (if/else) (condition ပေါ်မူတည်ပြီး လမ်းကြောင်း ရွေးခြင်း): code ထဲမှာ ဆုံးဖြတ်ချက် ချခြင်း
  • Iteration (Loops) (ထပ်ခါထပ်ခါ လုပ်ခြင်း): code ကို ထပ်ခါထပ်ခါ run ခြင်း

Variable နဲ့ type တွေကို နားလည်ပြီဆိုရင် နောက်တစ်ဆင့်အနေနဲ့ selection (if/else) ကို ဆက်လေ့လာရတာ သဘာဝကျပါတယ်။ score >= 50 လို condition (မှန်လား မှားလား စစ်တဲ့ အခြေအနေ) တွေကို ဘယ်လို ရေးရလဲ သိပြီးသား ဖြစ်နေလို့ပါ။