ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
String ဆိုတာ စာလုံးတွေကို အစဉ်လိုက် စီထားတဲ့ ပုတီးကြိုးတစ်ချောင်းလိုပါပဲ။ ပုတီးစေ့တိုင်းမှာ 0 ကနေ စတဲ့ နံပါတ်ရှိပြီး "Java" ဆိုရင် J က 0၊ a က 1 စသဖြင့်ပါ။ Username, email, search keyword, message, title — app ထဲက text အားလုံးက String ပါ။
အရေးကြီးတဲ့ စည်းကမ်းတစ်ခု ရှိပါတယ်: ပုတီးကြိုးကို ပြင်လို့ မရပါဘူး။ toUpperCase() လို method ကို ခေါ်ရင် စာလုံးကြီးနဲ့ ကြိုးအသစ်တစ်ချောင်း ပြန်ပေးပြီး မူရင်းကြိုးက ဒီအတိုင်း ကျန်ပါတယ်။ ရလဒ်ကို variable တစ်ခုထဲ ပြန်ထည့်မှ ပြောင်းလဲမှုကို ရပါမယ်။
Problem → Why → Solution
ပြဿနာ: password မှန်ရဲ့သားနဲ့ login မဝင်ဘူး
Login စစ်တဲ့ code ကို test လုပ်တုန်းက literal တွေနဲ့ စမ်းလို့ အလုပ်လုပ်ခဲ့ပါတယ်။ ဒါပေမယ့် တကယ့် input ကနေ ဝင်လာတဲ့ text ကို စစ်တော့ password မှန်နေရဲ့သားနဲ့ "Wrong password" ပြပါတယ်။ နောက်ထပ် bug တစ်ခုကလည်း username ကို စာလုံးအသေး ပြောင်းထားတယ်လို့ ထင်ပေမယ့် မပြောင်းသေးပါဘူး။
public class Main {
public static void main(String[] args) {
String saved = "secret";
String typed = "sec";
typed = typed + "ret"; // built at runtime, like real input
String user = " Aye ";
user.strip().toLowerCase(); // result thrown away
System.out.println("[" + user + "]"); // still " Aye "
if (typed == saved) { // compares references, not text
System.out.println("Welcome");
} else {
System.out.println("Wrong password");
}
}
}ဘာကြောင့် ဒီလို ဖြစ်တာလဲ
String က object ဖြစ်လို့ == က "ဒီ variable နှစ်ခုက memory ထဲက object တစ်ခုတည်းကို ညွှန်နေလား" ကိုပဲ စစ်ပါတယ်။ Runtime မှာ ဆက်လိုက်တဲ့ string က object အသစ်ဖြစ်လို့ စာသား တူနေရင်တောင် false ရပါတယ်။ စာသားကို နှိုင်းယှဉ်ချင်ရင် equals() ကို သုံးရပါမယ်။ strip() line ကတော့ String တွေ immutable ဖြစ်လို့ ရလဒ်ကို user = ... လို့ ပြန် assign မလုပ်ရင် ဘာမှ မပြောင်းပါဘူး။
Core concept: immutable object နဲ့ index
Java မှာ String က primitive type မဟုတ်ဘဲ class တစ်ခုပါ၊ ဒါကြောင့် စာလုံး S အကြီးနဲ့ ရေးပြီး method တွေ ခေါ်လို့ရပါတယ်။ Character တွေကို 0 ကနေ index ပေးထားလို့ "Java Tutorial" မှာ length() က 13 ဖြစ်ပြီး နောက်ဆုံး index က 12 ပါ။ charAt(i) က index တစ်ခုက character ကို ပေးပြီး range ပြင်ပ index ပေးရင် StringIndexOutOfBoundsException တက်ပါတယ်။ String အတွက် length() က method ဖြစ်လို့ ကွင်းပါပါတယ်၊ နောက် lesson မှာ တွေ့မယ့် array ရဲ့ length ကတော့ ကွင်းမပါတဲ့ field ပါ။
String တွေက immutable ပါ — ဖန်တီးပြီးရင် ပြင်လို့ မရပါဘူး။ toUpperCase(), strip(), replace(), substring() စတဲ့ method တိုင်းက String အသစ် ပြန်ပေးပါတယ်။ ဒါကြောင့် method တွေကို ဆက်ခေါ်လို့ (chain) ရပါတယ်: email.strip().toLowerCase()။ indexOf("@") က ပထမဆုံး တွေ့တဲ့ index ကို ပေးပြီး မတွေ့ရင် -1 ပြန်ပေးပါတယ်။ substring(begin, end) က begin ပါပြီး end မပါတဲ့ အပိုင်းကို ဖြတ်ယူပါတယ်၊ substring(begin) ကတော့ အဆုံးထိ ယူပါတယ်။
strip() က အရှေ့အနောက် whitespace ကို ဖယ်ပါတယ်။ trim() လည်း ရှိပေမယ့် strip() က Unicode whitespace တွေကိုပါ သိလို့ ခေတ်မီ code မှာ ပိုသင့်တော်ပါတယ်။ isBlank() က space တွေပဲ ပါတဲ့ string အတွက် true ပေးပါတယ်။
နှိုင်းယှဉ်ခြင်းမှာ == က reference နှစ်ခု တူမတူ (object တစ်ခုတည်းလား) ကို စစ်ပြီး equals() က character တွေ တူမတူ စစ်ပါတယ်။ "java" လို literal တွေကို Java က string pool ထဲမှာ မျှသုံးလို့ literal နှစ်ခုကို == နဲ့ နှိုင်းရင် တိုက်ဆိုင်ပြီး true ရတတ်ပါတယ် — ဒါကြောင့် ဒီ bug က test မှာ မပေါ်ဘဲ production မှာမှ ပေါ်တာပါ။ စာလုံးကြီးအသေး မခွဲချင်ရင် equalsIgnoreCase() ကို သုံးပါ။ Variable က null ဖြစ်နိုင်ရင် "admin".equals(role) လို literal ဘက်ကနေ ခေါ်ပါ၊ role.equals("admin") က role null ဆိုရင် NullPointerException တက်ပါတယ်။
| Method | ဘာပြန်ပေးလဲ |
|---|---|
| s.length() | character အရေအတွက် (int) |
| s.indexOf("x") | ပထမဆုံး index၊ မတွေ့ရင် -1 |
| s.substring(b, e) | b ကနေ e မပါခင်အထိ String အသစ် |
| s.strip() | အရှေ့အနောက် whitespace ဖယ်ထားတဲ့ String အသစ် |
| s.equals(t) | စာသား တူရင် true |
| s.contains("x") | ပါရင် true |
- မူရင်း String ကို ဖတ်ယူ
- ရလဒ်အတွက် String အသစ် ဖန်တီး
- String အသစ်ကို return ပြန်
- မူရင်းက မပြောင်းဘဲ ကျန်
ရလဒ်ကို variable ထဲ မထည့်ရင် အလုပ်လုပ်ခဲ့သမျှ ပျောက်သွားပါတယ်။
အထွေထွေ ပုံစံ
String text = "literal";
int size = text.length();
String changed = text.method(arguments); // new String; text is unchanged
int position = text.indexOf("part"); // -1 when not found
String piece = text.substring(start, end); // end is exclusive
boolean same = text.equals(other); // compare content, never ==
boolean safe = "literal".equals(maybeNull); // null-safe comparisonString ကို literal နဲ့ ဖန်တီးပြီး method တွေကို dot (.) နဲ့ ခေါ်ပါတယ်။ ပြောင်းလဲတဲ့ method တိုင်းက String အသစ် ပြန်ပေးလို့ ရလဒ်ကို variable ထဲ ထည့်ရပါမယ်။ စာသား နှိုင်းယှဉ်ရင် equals() ကို သုံးပြီး null ဖြစ်နိုင်တဲ့ value ကို argument ဘက်မှာ ထားပါ။
Example: email ကို သန့်စင်ပြီး ခွဲထုတ်ခြင်း
User တစ်ယောက် form ထဲမှာ space တွေ၊ စာလုံးကြီးတွေ ရောပြီး email ရိုက်ထည့်တယ် ဆိုပါစို့။ ဒီ example က email ကို သန့်စင်ပြီး @ နေရာကို ရှာကာ user နဲ့ domain နှစ်ပိုင်း ခွဲထုတ်ပါတယ်။ နောက်ဆုံး line နှစ်ကြောင်းက == နဲ့ equals() ကွာခြားချက်ကို မျက်မြင် ပြထားပါတယ်။
public class Main {
public static void main(String[] args) {
String title = "Java Tutorial";
String email = " Student@Example.com ";
System.out.println("Length: " + title.length());
System.out.println(title.toUpperCase());
System.out.println("Title unchanged: " + title);
String clean = email.strip().toLowerCase();
System.out.println("Clean: [" + clean + "]");
int at = clean.indexOf("@");
System.out.println("@ at index: " + at);
System.out.println("User: " + clean.substring(0, at));
System.out.println("Domain: " + clean.substring(at + 1));
String course = "ja";
course = course + "va";
System.out.println("course == java? " + (course == "java"));
System.out.println("course.equals(java)? " + course.equals("java"));
}
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Lines 7-8
System.out.println(title.toUpperCase());toUpperCase() က "JAVA TUTORIAL" ဆိုတဲ့ String အသစ်ကို ပြန်ပေးပြီး print ထုတ်ပါတယ်။ နောက် line က title ကို ထပ် print ထုတ်တော့ "Java Tutorial" အတိုင်း ရှိနေတာကို တွေ့ရပါတယ် — immutable ဆိုတာ ဒါပါပဲ။
- Line 10
String clean = email.strip().toLowerCase();strip() က အရှေ့အနောက် space တွေ ဖယ်ထားတဲ့ String အသစ် ပြန်ပေးပြီး အဲဒီ String ပေါ်မှာ toLowerCase() ကို ဆက်ခေါ်ပါတယ်။ နောက်ဆုံး ရလဒ်ကို clean ထဲ သိမ်းထားလို့ email မူရင်းက မပြောင်းပါဘူး။
- Line 12
int at = clean.indexOf("@");"student@example.com" မှာ student က index 0-6 ဖြစ်လို့ @ က index 7 မှာ ရှိပါတယ်။ @ မပါရင် -1 ရမှာမို့ တကယ့် code မှာ -1 ကို စစ်ရပါမယ်။
- Lines 14-15
System.out.println("Domain: " + clean.substring(at + 1));substring(0, at) က index 0 ကနေ 7 မပါခင်အထိ ယူလို့ "student" ရပါတယ်။ substring(at + 1) က @ ရဲ့ နောက် character ကနေ အဆုံးထိ ယူလို့ "example.com" ရပါတယ်။
- Lines 17-20
System.out.println("course == java? " + (course == "java"));course + "va" ကို runtime မှာ ဆက်လို့ object အသစ် ဖြစ်ပါတယ်။ == က pool ထဲက "java" literal နဲ့ object မတူလို့ false ပေးပြီး equals() က character တွေ တူလို့ true ပေးပါတယ်။
Length: 13
JAVA TUTORIAL
Title unchanged: Java Tutorial
Clean: [student@example.com]
@ at index: 7
User: student
Domain: example.com
course == java? false
course.equals(java)? trueStringBuilder, text block နဲ့ formatted()
String တွေ immutable ဖြစ်လို့ loop ထဲမှာ result = result + piece လို့ ထပ်ခါထပ်ခါ ဆက်ရင် အကြိမ်တိုင်း String အသစ် ဖန်တီးပြီး အဟောင်းကို copy ကူးရပါတယ်။ အပိုင်း ထောင်ချီ ဆက်ရင် နှေးလာပါတယ်။ StringBuilder က ပြင်လို့ရတဲ့ buffer ဖြစ်ပြီး append() နဲ့ ဆက်ကာ နောက်ဆုံးမှ toString() တစ်ခါ ခေါ်ပါတယ်။ Line အများကြီးပါတဲ့ text အတွက် text block (""" ... """) ကို သုံးပြီး placeholder တွေကို formatted() နဲ့ ဖြည့်လို့ရပါတယ်။ String.join() က item တွေကြားမှာ separator ထည့်ပေးပါတယ်။
public class Main {
public static void main(String[] args) {
StringBuilder line = new StringBuilder();
line.append("HTML").append(", ").append("CSS").append(", ").append("Java");
System.out.println(line.toString());
String report = """
Name: %s
Score: %d
""".formatted("Aye", 92);
System.out.print(report);
System.out.println(String.join(" | ", "HTML", "CSS", "Java"));
System.out.println("ha".repeat(3) + " " + " ".isBlank());
}
}Output က HTML, CSS, Java၊ Name: Aye၊ Score: 92၊ HTML | CSS | Java နဲ့ hahaha true ပါ။ Text block ရဲ့ ပိတ်တဲ့ """ ရှိတဲ့ indentation အထိ အရှေ့က space တွေကို Java က ဖယ်ပေးလို့ Name: က line အစကနေ စပါတယ်။
လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Sign-up form တိုင်းက email နဲ့ username ကို database ထဲ မသိမ်းခင် strip() နဲ့ toLowerCase() လုပ်ပါတယ် — ဒါမှ "Aye@Mail.com " နဲ့ "aye@mail.com" က account နှစ်ခု မဖြစ်မှာပါ။ Search box က user ရိုက်တာကို lowercase ပြောင်းပြီး contains() ဒါမှမဟုတ် database query နဲ့ ရှာပါတယ်။ File upload မှာ filename.lastIndexOf('.') နဲ့ extension ကို ခွဲထုတ်ပြီး ခွင့်ပြုထားတဲ့ type ဟုတ်မဟုတ် စစ်ပါတယ်။
Role, status code, command name တွေကို နှိုင်းယှဉ်တဲ့ code မှာ "ADMIN".equals(role) ပုံစံကို code review တွေမှာ မကြာခဏ တွေ့ရပါတယ်၊ request ထဲမှာ role မပါလို့ null ဖြစ်နေရင်တောင် crash မဖြစ်လို့ပါ။ Log message, email body, CSV row တွေ တည်ဆောက်တဲ့အခါ StringBuilder, String.join() နဲ့ text block တွေကို သုံးပါတယ်။ Password ကိုတော့ ဒီလို plain String နဲ့ မနှိုင်းယှဉ်ဘဲ hash လုပ်ထားတဲ့ value ကို security library နဲ့ စစ်တာက တကယ့် app တွေရဲ့ စံပါ — problem section က code ကို == bug ပြဖို့ပဲ ရိုးရှင်းအောင် ရေးထားတာပါ။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. Text ကို == နဲ့ နှိုင်းယှဉ်တာ
Literal နှစ်ခုကို == နဲ့ နှိုင်းရင် string pool ကြောင့် true ရတတ်လို့ test မှာ အောင်သလို ထင်ရပါတယ်။ Input, file ဒါမှမဟုတ် concatenation ကနေ လာတဲ့ String တွေမှာတော့ false ရပါတယ်။ Content ကို နှိုင်းယှဉ်ရင် equals() ဒါမှမဟုတ် equalsIgnoreCase() ကို အမြဲ သုံးပါ။
2. Method ရဲ့ ရလဒ်ကို လွှင့်ပစ်တာ
name.strip(); လို့ တစ်ကြောင်းတည်း ရေးရင် String အသစ်က ချက်ချင်း ပျောက်သွားပြီး name က မပြောင်းပါဘူး။ name = name.strip(); လို့ ပြန် assign လုပ်ပါ။ IDE တွေက "result of String.strip() is ignored" လို warning ပြတတ်လို့ အဲဒါကို လျစ်လျူ မရှုပါနဲ့။
3. substring() ရဲ့ end ကို ပါတယ်လို့ ထင်တာ
"Java".substring(1, 3) က "av" ပါ၊ "ava" မဟုတ်ပါဘူး — end index က မပါပါဘူး။ indexOf() က -1 ပြန်တဲ့အခါ substring(-1) လို ခေါ်မိရင်လည်း StringIndexOutOfBoundsException တက်လို့ -1 ကို အရင် စစ်ပါ။
Mini challenge
Mini challenge: contact card စစ်ဆေးသူ
Raw email နဲ့ နာမည်အပြည့်အစုံကို လက်ခံပြီး သန့်စင်ထားတဲ့ email၊ နာမည်ရဲ့ initials နဲ့ email ပုံစံ မှန်မမှန်ကို print ထုတ်တဲ့ program ရေးပါ။
- normalize(raw) — အရှေ့အနောက် space ဖယ်ပြီး lowercase ပြောင်းရမယ်
- looksLikeEmail(email) — @ တစ်ခုတည်း ပါရမယ်၊ ပထမ character မဖြစ်ရ၊ @ နောက်မှာ . တစ်ခု အနည်းဆုံး character တစ်လုံးခြားပြီး ရှိရမယ်
- initials(fullName) — ပထမ စကားလုံးနဲ့ ဒုတိယ စကားလုံးရဲ့ ပထမ စာလုံးကို စာလုံးကြီးနဲ့ (စကားလုံး တစ်ခုတည်းဆိုရင် တစ်လုံးတည်း)
- Input လေးမျိုးနဲ့ စမ်းပြီး expected output အတိုင်း ထုတ်ရမယ်
public class Main {
static String normalize(String raw) {
// TODO: strip, then lowercase
return raw;
}
static boolean looksLikeEmail(String email) {
// TODO: compare indexOf('@') with lastIndexOf('@'), then look for a '.'
return false;
}
static String initials(String fullName) {
// TODO: find the first space; it may not exist (-1)
return "";
}
static void report(String rawEmail, String fullName) {
// TODO: print "AC <aye@mail.com> valid: true"
}
public static void main(String[] args) {
report(" Aye@Mail.COM ", "aye chan");
report("no-at-sign.com", "Min");
report("a@b@c.com", " su myat ");
report("kyaw@school.edu.mm", "Kyaw Zin");
}
}ရလဒ်: Program က ဒီ line လေးကြောင်းကို ထုတ်ရပါမယ်: AC <aye@mail.com> valid: true M <no-at-sign.com> valid: false SM <a@b@c.com> valid: false KZ <kyaw@school.edu.mm> valid: true
အရိပ်အမြွက် ကြည့်ရန်
indexOf('@') နဲ့ lastIndexOf('@') တူရင် @ တစ်ခုတည်း ပါတာပါ။ indexOf('.', at) က @ နေရာကနေ စရှာပါတယ်။ Initials အတွက် နာမည်ကို strip() အရင်လုပ်ပြီး ဒုတိယ စကားလုံးရဲ့ စာလုံးကို substring(space + 1, space + 2) နဲ့ ယူပါ။
အဖြေ ကြည့်ရန်
public class Main {
static String normalize(String raw) {
return raw.strip().toLowerCase();
}
static boolean looksLikeEmail(String email) {
int at = email.indexOf('@');
boolean oneAt = at > 0 && at == email.lastIndexOf('@');
return oneAt && email.indexOf('.', at) > at + 1;
}
static String initials(String fullName) {
String name = fullName.strip();
int space = name.indexOf(' ');
String second = space < 0 ? "" : name.substring(space + 1, space + 2);
return (name.substring(0, 1) + second).toUpperCase();
}
static void report(String rawEmail, String fullName) {
String email = normalize(rawEmail);
boolean valid = looksLikeEmail(email);
System.out.println(initials(fullName) + " <" + email + "> valid: " + valid);
}
public static void main(String[] args) {
report(" Aye@Mail.COM ", "aye chan");
report("no-at-sign.com", "Min");
report("a@b@c.com", " su myat ");
report("kyaw@school.edu.mm", "Kyaw Zin");
}
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- String က 0 ကနေ index ပေးထားတဲ့ immutable object ဖြစ်ပြီး method တိုင်းက String အသစ် ပြန်ပေးပါတယ်။
- indexOf() က မတွေ့ရင် -1 ပြန်ပြီး substring(begin, end) မှာ end က မပါပါဘူး။
- Text ကို equals() နဲ့ နှိုင်းယှဉ်ပါ — == က object တစ်ခုတည်းလား ဆိုတာပဲ စစ်ပါတယ်။
- User input ကို strip() နဲ့ toLowerCase() လုပ်ပြီး text များများ စုရင် StringBuilder သုံးပါ။